Instead of printing an error message and dying the whole php process, you should rather throw another exception and register a global exception handler that handles the exception for unhandled exceptions.
class CustomException extends \Exception {
}
class FirstClass {
function method() {
try {
$get = external();
if (!isset($get['ok'])) {
throw new CustomException;
}
return $get;
} catch (Exception $ex) {
throw $ex;
}
}
}
class SecondClass {
function get() {
try {
$firstClass = new FirstClass();
$get = $firstClass->method();
} catch (CustomException $e) {
throw $e;
}
}
}
$secondClass = new SecondClass();
$secondClass->get();
You can register a global exception handler with set_exception_handler
set_exception_handler(function ($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "\n";
});
source
share