How to handle Exception in PHP

Earlier versions of PHP version did not have a method to handle “No Exception Handle”. PHP 5.0 and above today supports this function by using catch blocks for errors and exception handling.

Some Basics about Exception: 

1-PHP throw and PHP catch:

      This method will work by default by using PHP try catch. The program execution will stop and display an error message that the programmer can easily understand.

2-PHP throw and our catch:

How try-catch block works are explained below. When PHP throws exception (eg. divide any number with 0) we will catch that exception after the try block. In PHP we have limited exception list. If the programmer desired to customize exception that option is available below. But Important thing after exception our program will not end.

3-OUR throw and PHP catch:

    If We just throw error and PHP will catch the exception. We can customize the exception but after the exception, our program execution will stop and display an error message. This is not an advisable method.

4-OUR throw and our catch:

        We can throw and catch any type of exception. This Method is used across all programming languages. We can also customize error message and exception. In this method, program execution will not stop after exception, display error message and execute after catch block code.

Note: If we are not using catch block then after exception definitely our program will stop and display programmer understandable error message.  

Try, Catch, Finally, Throw:-

try {
// start code to run here
}
catch (Exception $ex) {
echo $ex->getMessage(); //First Catch block
}
catch (InvalidArgumentException $ex) {
echo $ex->getMessage(); //Second Catch block
}
finally {
// finally block is optional
}

Try:

This block contains all who have come exception on executed time. If exception comes then this block will stop to execute.

Throw:

In the Throw keyword, we used occurrence exception in PHP and use try, catch in runtime.

Catch:

The Catch block we pass exception type and write to handle code inside the catch block.

Finally:

This Finally blocks come in PHP version 5.5 This block we have to write special Syntex. Finally, create a block of code that will be executed after a try(if catch not available) or catch block has completed and before the code following the try or catch block. Finally, the block will execute if no catch statement matches the exception.

How to handle multiple type exception in PHP:-

After PHP version 5.5 we can write multiple catch blocks in one try. PHP allows customizing code according to the type of exception. We can also customize the error code also.

try {
// start code to run here
}
catch (Exception $ex) {
echo $ex->getMessage(); //First Catch block
}
catch (InvalidArgumentException $ex) {
echo $ex->getMessage(); //Second Catch block
}
finally {
// finally block is optional
}

In PHP Exception handling provides a powerful mechanism for controlling complex programs that have many dynamic run-time characteristics.

A try and is catch statement form a unit. The statement that is protected by try must be use {}. You can not use try on a single statement.

Creating our type PHP Exception:-

PHP we can create our type exception. This is very useful for any application that you can have special exception handling around. In our program we create functions for exception occur than execute. The class (DivideByNegativeException) should be an extension of the exception class.

class DivideByNegativeException extends Exception {};

class DivideByZeroException extends Exception {};function divide_process($denominator)
{
try
{
if ($denominator < 0)
{
throw new DivideByNegativeException();
}
else if ($denominator == 0)
{
throw new DivideByZeroException();
}
else
{
echo 25 / $denominator;
}
}
catch (DivideByZeroException $e)
{
echo “This program has Divided by zero exception!”;
}catch (DivideByNegativeException $e)
{
echo “This program Divide by negative number exception!”;
}catch (Exception $e)
{
echo “This program have UNKNOWN EXCEPTION!”;
}
}

This code have custom type exception class. The DivideByNegativeException() and DivideByZeroException() classes are created as extensions of the existing Exception class; this way, it inherit all method and properties from the Exception class. Try block is executed and exception is thrown if denominator is negative or zero number. The catch block catch the exception and display the error message.

Nested try Statement:

       In PHP try statement can be nested, It means inside try statement we can use one or more try statement. Every time try statement pushed exception and we use one or multiple catch block outside  main try block. If any exception come in any try block then all try block  execution stop and our program automatic redirect to catch block if exception handle propery then error message display and program not end  otherwise program excecution stop.

We can also configure global PHP exception handler, we will use the set_exception_handler() function to set a user-defined function to handle all uncaught exceptions.

function global_exception_handler($exception)
{
echo “This program Exception:” . $exception->getMessage();
}
set_exception_handler(‘global_exception_handler’);