1

I can't catch the error with the Exception PHP class.

<?php
require "vendor/autoload.php";

use App\Events\Logger;
use Exception;
/* Alterntly id tryed to use this call but dosnt work either use \Exception as Exception; */

$logger = new Logger("error.log", "ERROR");

try {
    new DosentExist(); //I force the exception.
} catch (Exception $e) {
    echo "error";
    $error = new Error($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
    $logger->log((string) $error, "ERROR");
}

Console error:

$ php index.php
Fatal error: Uncaught Error: Class "DosentExist" not found in C:\code\php\logs-forfun\index.php:10
Stack trace:
#0 {main}
  thrown in C:\code\php\logs-forfun\index.php on line 10

1 Answers1

3

If you read the error message, you'll see that it does not mention Exception, but rather mentions an "Uncaught Error". You'd need to catch an Error, not an Exception:

try {
    new DosentExist(); //I force the exception.
} catch (Error $e) {
    echo "error";
}
Wongjn
  • 8,544
  • 2
  • 8
  • 24