This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Now that we have our errors showing, we’re ready to get started actually addressing those errors. Errors can be output to the screen, local disk or both. PHP can also choose which error severities it will report and which it will ignore. We'll look at how to use those errors to fix your code.
Custom Function
You can define a custom function to handle errors.
function customError($errno, $errstr) {
echo "<b>Error:</b> [$errno] $errstr<br>";
echo "Ending Script";
die();
}
set_error_handler(“customError”);
Trigger an Error
You can trigger your own errors when something doesn't meet certain requirements. PHP comes with a built-in function for this purpose called trigger_error().
$test=2;
if ($test>1) {
trigger_error("Value must be 1 or below");
}
Errors Settings
Constant | Description |
---|---|
E_ERROR (integer) | Fatal run-time errors. These indicate errors that can not be recovered from, such as a memory allocation problem. Execution of the script is halted |
E_WARNING (integer) | Run-time warnings (non-fatal errors). Execution of the script is not halted |
E_PARSE (integer) | Compile-time parse errors. Parse errors should only be generated by the parser |
E_NOTICE (integer) | Run-time notices. Indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script |
E_CORE_ERROR (integer) | Fatal errors that occur during PHP's initial startup. This is like an E_ERROR, except it is generated by the core of PHP |
E_CORE_WARNING (integer) | Warnings (non-fatal errors) that occur during PHP's initial startup. This is like an E_WARNING, except it is generated by the core of PHP |
E_COMPILE_ERROR (integer) | Fatal compile-time errors. This is like an E_ERROR, except it is generated by the Zend Scripting Engine |
E_COMPILE_WARNING (integer) | Compile-time warnings (non-fatal errors). This is like an E_WARNING, except it is generated by the Zend Scripting Engine |
E_USER_ERROR (integer) | User-generated error message. This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error() |
E_USER_WARNING (integer) | User-generated warning message. This is like an E_WARNING, except it is generated in PHP code by using the PHP function trigger_error() |
E_USER_NOTICE (integer) | User-generated notice message. This is like an E_NOTICE, except it is generated in PHP code by using the PHP function trigger_error() |
E_STRICT (integer) | Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code |
E_RECOVERABLE_ERROR (integer) | Catchable fatal error. It indicates that a probably dangerous error occurred, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR |
E_DEPRECATED (integer) | Run-time notices. Enable this to receive warnings about code that will not work in future versions |
E_USER_DEPRECATED (integer) | User-generated warning message. This is like an E_DEPRECATED, except it is generated in PHP code by using the PHP function trigger_error() |
E_ALL (integer) | All errors and warnings, as supported, except of level E_STRICT prior to PHP 5.4.0 |
Exception Handling
In general Errors are the Procedural approach to handling errors, while Exceptions are the Object-Oriented approach. Exceptions are represented in PHP by the class Exception. Exceptions are raised using the “throw” syntax and can be caught using a “try/catch” syntax block. Any code in which an Exception error may occur should be placed in the “try” block, which can be followed by a “catch” block to handle the error.
This is what the Exception syntax looks like:
try { // throw errors in the try-block
// if an error occurs we can throw an exception
throw new Exception('this is an error.');
}
catch(Exception $e) { // catch the throws in the catch-block
// do something with the exception object, eg. display its message
echo 'Error message: ' .$e->getMessage();
}
http://code.tutsplus.com/tutorials/php-exceptions--net-22274
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up