Try/Catch Block in PHP Not Catching Exception

php: try-catch not catching all exceptions

Solution #1

Use ErrorException to turn errors into exceptions to handle:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("exception_error_handler");

Solution #2

try {
// just an example
$time = 'wrong datatype';
if (false === $timestamp = date("Y-m-d H:i:s", $time)) {
throw new Exception('date error');
}
} catch (Exception $e) {
return false;
}

PHP try-catch not working

You can't handle Warnings/Errors with try-catch blocks, because they aren't exceptions. If you want to handle warnings/errors, you have to register your own error handler with set_error_handler.

But it's better to fix this issue, because you could prevent it.

php try catch not catching exception

A Catchable Fatal Error is not an Exception, it is an Error. It cannot be caught by a try/catch block, but by a custom error handler. See this answer for more info.

This line:

echo $qry;    //invalid operation, I expect exception to be thrown

You are correct that it is invalid, but incorrect that an Exception is thrown. Unless an object has a valid __toString() method defined, which PDOStatement does not, attempting to cast as a string will generate a PHP error, not an Exception. This is because the code attempting to cast the object to a string is part of the PHP core, and not some bit in a class somewhere.

If you want to look at the query contained in a PDOStatement object you need to access the read-only property $stmt->queryString.

PHP Catch Not Catching Exceptions with Custom Exception Handler

 try {
$response = $client->GetAllOrdersByOrderNo($request);
} catch (Exception $e) {
// This code is never set
$response = null;
}

You are in namespace Base\Members\Library.

The block is catching Base\Members\Library\Exception

Try catching \Exception in the global namespace instead.

why is try catch not working? What am I doing wrong? (php)

It's not working because a failed include doesn't throw an exception, it throws a warning. Therefor the catch block will never be executed, as you'll only enter it if there is an exception. You can just check if the file exists, and if it doesn't, throw an exception.

try {
$page = 'pages/' . $c . '.php';

if (!file_exists($page))
throw new Exception('File does not exist: ['.$page.']');

include $page;
$content = getContent();

} catch (Exception $e){
$content = 'Error: '.$e->getMessage();
}

If the targeted file doesn't exist, it will output

Error: File does not exist: [path-to-file]

in your $content variable.

Reference

  • PHP Exceptions
  • file_exists() manual

PHP try-catch block not catching issues as expected

This is because Catchable Fatal Error are errors that leave the engine in unstable state so the execution should be aborted.

Think about it: after a "normal" exception you can, sometimes, recover program execution flow whereas under this circumstance is preferable to abort the execution.

If you want however, you can define your own error handler and let the execution keep going

Exception not able to get caught in catch block - Yii2

catch (\Throwable $e) will do the job

\Throwable was introduced back in PHP 7.0 and is (quoting from docs) used for

[...] any object that can be thrown via a throw statement, including
Error and Exception.



Related Topics



Leave a reply



Submit