ChennaiHost-Contact Number

Exception Handling in PHP

Published: Jun 20, 2013


Exception handling is a method that separates code that detects and handles exceptional circumstances from the rest of your program. Note that exceptional circumstances is not necessarily an error, When a function detects an exceptional condition, you represent this with an object. This object is called an exceptions entity. In order to deal with the exceptional situation you throw the exception. Then pass that control, as well as the exception, to a selected block of code in a direct or indirect caller of the function that threw the exception . This block is called handler.

In a coach(handler), you specify the types of exceptions that it may process. Run time environment will pass control to the first appropriate handler that is able to process the exception thrown. When this done an exception is caught. A handler may re throw an exception so it can be caught by another handler.The exception handling method is made up of the following elements:

  • The Try Block
  • Catch block
  • Throw the exception
  • The Try Block

You use a try block to indicate which area in your program that might throw exceptions you want to handle instantly. You use a function try block to indicate that you want to detect exception in the entire body of a function.

Syntax of try block
Set of statements (where the chances of error)


Example of try block

< ?php try { $error1 = ‘when error occur every time throw this error’; throw new Exception($error1); // when exception is not executed. echo ‘Never executed’; } ? >


Catch Block

You can declare a handler to catch many types of exceptions. The allowable objects that functions can catch are declared in the parentheses following the catch keyword (the exception_declaration). You can catch object of the fundamental types, base and derived objects, references and pointers to all of these types.

You cannot define a type in an exception declaration. You can also use the catch(……) from of the handler to catch all thrown exceptions that have not been caught by a preceding catch block. The contraction in the catch argument indicates that any exception thrown can be handled by this handler.

If the exception is trapped by a catch (…..) block, there is no direct way to access the entity thrown. Information about an exception trapped by catch(….) is very limited.

You can declare an optional variable name if you want to access the thrown object in the catch block. A catch block can only must have an accessible copy constructor

Syntax of catch block:-Catch (exception_class object name)
Body of the catch block

Example of Catch Block

catch (Exception $err)
throw new Exception( ‘Something going wrong’, 0, $err);

Throw Exception

You can use a throw expression to indicate that your program has encountered an exception. By default run time environment of the language automatically create appropriate object of exception class and throw it. If in case we have to create the object of appropriate class the explicitly and throw it. Then we use throw keyword.

Syntax of thrown exception

Throw new exception class();


A complete example or working of exception handling (php script).

< ?php //create function with an exceptionfunction checkNum($number) {

throw new Exception(“Value must be 1 or below”);
return true;

//trigger exception in a “try” block, just get it carefully

//If the exception is thrown, this text will not be shown
echo ‘If you see this, the number is 1 or below’;
//working of catch exception
catch(Exception $e)
echo ‘Message: ‘ .$e->getMessage();
? >