PHP Exception

Exception handling is the method of responding to the occurrence of errors during the program execution. Exceptions are used to change the normal flow of a script if a specified error condition occurs. The basic terms use in php for exception handling are :

Try : A function or statement using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown".

Throw : The exception in php is triggered by throw statement. Each "throw" must have at least one "catch".

Catch : A "catch" block retrieves an exception and creates an object containing the exception information.

When an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block. If an exception is not caught, a PHP Fatal Error will be issued with an Uncaught Exception.

An exception can be thrown and caught ( catch ) within PHP code. Each try must have at least one corresponding catch block. Multiple blocks can be used to catch different classes of exceptions.

The basic syntax of try catch() block is as follows :
try {
  // code to execute 
  // Exception is thrown from here
} catch(e) {
  // show exception message
}
The exception is thrown by the below statement :
 throw new Exception("message");
Where message is nothing but the error message or exception message you want to display. Now lets see some examples of exception handling :

Example 1 :
<?php

  try {
    $num = 10;
    $msg = "This is the Error Message.";
    
    if($num > 5) {
      throw new Exception($msg);
    }

    // The below code will never execute
    echo "hello world";
    echo "This is Exception handling demo.";
  
  } catch (Exception $e) {
    echo "Error Message : " . $e->getMessage();
  }

?>
Output :

Error Message : This is the Error Message.

In the above example the exception is thrown and $e->getMessage function is used to get error message. There are following functions which can be used from Exception class.

getMessage() − message of exception
getCode() − code of exception
getFile() − source filename
getLine() − source line
getTrace() − n array of the backtrace()
getTraceAsString() − formated string of trace

Example :
<?php

  try {
    $num = 10;
    $msg = "This is the Error Message.";
    
    if($num > 5) {
      throw new Exception($msg);
    }

    // The below code will never execute
    echo "hello world";
    echo "This is Exception handling demo.";
  
  } catch (Exception $e) {
    echo "Error Message : " . $e->getMessage() . "<br/>";
    echo "Source File Name : " . $e->getFile() . "<br/>";
    echo "Line From which exception thrown : " . $e->getLine() . "<br/>";
  }

?>
Output :

Error Message : This is the Error Message.
Source File Name : /home/user/myLab/phpCodes/test.php
Line From which exception thrown : 8

Example 2 :
<?php

  function calculate($num) {
    if($num<0) {
      throw new Exception("Value must be positive number.");
    }
    return $num*10;
  }

  try {
    calculate(-1);
    // The below code will never execute
    echo "hello world";
    echo "This is Exception handling demo.";
  } catch(Exception $e) {
    echo 'Error Message : ' . $e->getMessage() . "<br/><br/>";
  }

  // Code after try-catch block
  echo "This is the code after try-catch block.";

?>
Output :

Error Message : Value must be positive number.

This is the code after try-catch block.


Next Topic :