PHP Exception Handling

PHP Exception Handling tutorial will explain you what is exception handling in PHP? with example. Exceptions make it simple to interrupt program flow on the off chance that something goes wrong. They permit you to modify how a program handles errors, and smoothly debases an application.

What is an exception?

An Exception is an object that is “thrown” by your application. When an Exceptions is thrown, it halts processing until the Exceptions is either caught, or left unhandled.

Exceptions are the pieces of code which are used to alter the normal flow of the code if a certain error occurs. The concept of exceptions is being used in almost all high level languages.

Whenever an exception occurs, the current code state is saved and code execution is switched to a predefined (custom) exception handler function.

Exception without try catch block

Provided that you endeavor to toss a special case from a PHP destructor or a PHP special case handler, your script will execute fine for the most part and after that accident with the rather cryptic error inform, “Fatal error: Exception flung without a stack casing in Unknown on line 0”.

This message is unfortunately very cryptic if you are trying to debug your code.

Example of exception without try block:

The code above will get an error like this:

Exception handling using try and catch block

In PHP there are three blocks in Exceptions Handling : try, catch, and throw.

  • Try : The try block should have a function which is used to handle the exception.
  • Throw : This block is used to trigger an exception. Each throw must have at least one “catch”.
  • Catch : This block is used to retrieve an exception and instantiates an object, which contains the exception message.

Example to demonstrate PHP exception handling:

In this example, PHP enters the “try” block and starts executing code. When it hits the line “throw new Exception”, it will stop executing the “try” block, and jump to the “catch” block. Here it checks each exception option against the list in “catch” and executes the appropriate code. Once PHP has left the “try” block, it will not return to it, which means that the line $foo = “bar” will never be executed.

Creating Custom Exception Class

PHP will let you throw any object as if it were an exception, but as a rule of thumb the exception should extend PHP’s built-in Exceptions class. Based on the object’s type, you can handle the exceptions differently.

Custom exception handling can perform suitable actions like logging error messages in file, providing exact details about the line on which the error occurred by examining the calling stack, etc.

Example:

The code above defines two new custom exception types, NameException and EmailException, which can be used to indicate different errors. Then within the try block, the code checks if values have been supplied for the variables $name and $email. If either is empty, then the appropriate exception is thrown using throw. The corresponding catch block is executed which handles the error.

Re-throwing Exception

try/catch blocks can be nested. Sometimes you’ll want to catch an exception, look at some of its properties, and then throw it again to let a parent catch block handle it.

This can often be useful to check an error condition and decide whether it should be fatal or not.

Example demonstrates re-throwing an exception:

Uncaught Exception Handler

Similar to how set_error_handler() allows you specify a function to handle run-time errors, the set_exception_handler() function allows you to handle exceptions that make it all the way up the call stack without being caught by any catch blocks.

For example, if an exception makes it all the way up your stack, it would be a good idea to log it in a log file. You can create a callback function and register it with set_exception_handler() as shown in the example below:

SHARE
Previous articlePHP Error Handling
Next articlePHP Filter
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!