Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Create your own Exception handler in cakephp ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 436
    Comment on it

    Hello readers, today we will discuss a very important topic of a programming language "exception handling". Exception Handling is the process of catching errors which are generated by our program and then taking appropriate action according to our requirement. Exceptions are some additional flexibility  in our program because  exceptions can be extended to some extent and they contain some logic also. When an exception is triggered the current code is saved and the execution will switch  to your exception handler function rather than halting the execution of program .

     

    The in-built exception handling in CakePHP will capture any uncaught exceptions and render a useful page. There are some in-built exceptions inCakePHP.

     

    1.  exception ForbiddenException: is used for doing a 403 Forbidden error.

    2.  exception NotImplementedException: is used for doing a 501 Not Implemented Errors.

    3.  exception NotFoundException: is used for doing a 404 Not found error.

    4.  exception BadRequestException: is used for doing 400 Bad Request error.

    5.  exception UnauthorizedException: is used for doing a 401 Unauthorized error.

    6.  exception MethodNotAllowedException: is used for doing a 405 Method Not Allowed error.

    7.  exception InternalErrorException: is used for doing a 500 Internal Server Error.

     

     


    You can create your  own exception handler which gives you full control over the exception handling process. First, load the class in your app/Config/bootstrap.php, now it is available to handle any exceptions. You can define th handler as any callback type. By settings Exception.handler CakePHP will ignore all other Exception settings. A sample custom exception handling setup could look like:

    App::uses('AppExceptionHandler', 'Lib');
    
    
    class AppExceptionHandler {
     public static function handle($error) {
            echo 'Oh God! ' . $error->getMessage();
            
        }
        
    }
     


    If we run the above code, the output will be  :  

    "Oh God"    plus exception message

     

    If you are creating custom exception handler you can provide custom error handling  in your exceptions. You can run any code inside handleException. If you don’t want to take control of the exception handling, but only want to change how exceptions are rendered you can use

     

    
     Configure::write('Exception.renderer', 'AppExceptionRenderer');

     

     

      

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: