Friday, March 29, 2019

How to hundle exception in php

An exception is unexpected program result that can be handled by the program itself. Exception Handling in PHP is almost similar to exception handling in all programming languages.
PHP provides following specialized keywords for this purpose.
  • try: It represent block of code in which exception can arise.
  • catch: It represent block of code that will be executed when a particular exception has been thrown.
  • throw: It is used to throw an exception. It is also used to list the exceptions that a function throws, but doesn’t handle itself.
  • finally: It is used in place of catch block or after catch block basically it is put for cleanup activity in PHP code.
  • Why Exception Handling in PHP ?
    Following are the main advantages of exception handling over error handling
    • Separation of error handling code from normal code: In traditional error handling code there is always if else block to handle errors. These conditions and code to handle errors got mixed so that becomes unreadable. With try Catch block code becomes readable.
    • Grouping of error types: In PHP both basic types and objects can be thrown as exception. It can create a hierarchy of exception objects, group exceptions in namespaces or classes, categorize them according to types.
    Exception handling in PHP:
    • Following code explains the flow of normal try catch block in PHP:
  • <?php 

  • // PHP Program to illustrate normal 
  • // try catch block code 
  • function demo($var) { 
  • echo " Before try block"; 
  • try { 
  • echo "\n Inside try block"; 
  • // If var is zero then only if will be executed 
  • if($var == 0) 
  • // If var is zero then only exception is thrown 
  • throw new Exception('Number is zero.'); 
  • // This line will never be executed 
  • echo "\n After throw (It will never be executed)"; 
  • // Catch block will be executed only 
  • // When Exception has been thrown by try block 
  • catch(Exception $e) { 
  • echo "\n Exception Caught", $e->getMessage(); 
  • // This line will be executed whether 
  • // Exception has been thrown or not 
  • echo "\n After catch (will be always executed)"; 

  • // Exception will not be rised 
  • demo(5); 

  • // Exception will be rised here 
  • demo(0); 
  • ?> 
Output
Before try block Inside try block After catch (will be always executed) Before try block Inside try block Exception CaughtNumber is zero. After catch (will be always executed)

No comments:

Post a Comment