[wd_asp elements=’search’ ratio=’100%’ id=1]

How to catch errors and warnings in PHP

8th December 2020

Php - Miscellaneous

php codehaven

Catching errors and Warnings

Below is a standard for catching errors. This would not work for warnings. That code is at the bottom.

try {
print "this is our try block n";
throw new Exception();
} catch (Exception $e) {
print "something went wrong, caught yah! n";
} finally {
print "this part is always executed n";
}

How to catch warnings


set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{
throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
}, E_WARNING);

try {
// any code that may have a warning
}

//restore the previous error handler
restore_error_handler();