Using Exception Objects to Handle Errors In PHP

PHP gives an exceptions, which are used for triggering and handling error conditions. Rather than returning a single error value, the method or function can create a rich Exception object that includes detailed information about the problem and then throw the object up to the calling code to handle, or catch.

Another nice feature of an exceptions is that the calling code does not have to catch an exception if it does not want to; if it ignores it, the exception is re – thrown up the calling chain until it is caught. If no code catches the exception, the script halts with a fatal error and the exception is logged or displayed to the user.

By using exceptions, any problem can either be handled automatically by another part of the application or, if all else fails, reported to the developer or user. This allows applications to be much more flexible and robust in their handling of error scenarios.

If users do not want uncaught exceptions to raise fatal errors, then they can create their own exception handler to deal with the exceptions (much like creating their own error handler).

Throwing Exceptions

Here you will learn how to create and throw an exception when an error occurs in the code:

throw new Exception;

A user can also pass an optional error message to the Exception object when it is created:

throw new Exception(“Oops, I think something went wrong”);

If a user have a lot of different error messages in the application, it can help to give each exception a numeric error code to distinguish it. To add an error code to the thrown exception, pass it as the second argument when creating the Exception object:

throw new Exception(“Oops, I think something went wrong”, 111);

If users do not catch the thrown exception at some other point in the code, eventually it bubbles up to the top level of the script, displaying an error message similar to the following:

PHP Fatal error: Uncaught exception ‘Exception’ with message ‘Oops,
something went wrong’ in script.php:4
Stack trace:
#0 {main}
thrown in script.php on line 4

This tells a user that an exception occurred that was not handled by the script itself, gives the error message, and informs that the exception was thrown in the main (top – level) part of the script.

Catching Exceptions

To catch an exception in the script. A user use a try … catch block, as follows:

try {
// Call the function or method
} catch ( Exception $e ) {
// Handle the exception
}

The code between try and catch is run. Often this includes a call to a function or an object method. If this code results in an exception being thrown, the code after catch is run. The catch construct expects a parameter, which is the thrown Exception object ( $e in this example). It is up to you how a user then handle the exception. A user might simply exit the script with an error message:

die( “There was a problem.” );

Alternatively, a user can query the Exception object to find out more about the problem. All Exception objects contain the following methods that a user can use to get more information:

Exception Method Description
getMessage() This method returns the error message contained in the exception.
getCode() This method returns the error code contained in the exception.
getFile() This method returns the name of the script file where the exception occurred.
getLine() This method returns the line number within the script file where the exception occurred.
getTrace() This method returns an array showing the nesting of the functions and/or method calls that led to the exception.
getTraceAsString() This method returns a formatted string showing the nesting of the functions and/or method calls that led to the exception.

if the exception was not that serious, a user can simply display the exception’s error message and carry on as normal:

try {
// Call the function or method
} catch ( Exception $e ) {
echo $e- > getMessage();
}

If no exception occurred within the try … catch block, the PHP engine simply carries on with the script, starting at the line after the try … catch block.