Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get exception context in PHP

If you use a custom error handler in PHP, you can see the context of an error (the value of all variables at the place where it occurred). Is there any way to do this for exceptions? I mean getting the context, not setting an exception handler.

like image 468
Bart van Heukelom Avatar asked Nov 27 '09 15:11

Bart van Heukelom


People also ask

Why we use try catch in PHP?

PHP supports using multiple catch blocks within try catch. This allows us to customize our code based on the type of exception that was thrown. This is useful for customizing how you display an error message to a user, or if you should potentially retry something that failed the first time.

What are exceptions in PHP?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.

What is Exception Handling in PHP explain?

Exception handling is a powerful mechanism of PHP, which is used to handle runtime errors (runtime errors are called exceptions). So that the normal flow of the application can be maintained. The main purpose of using exception handling is to maintain the normal execution of the application.


3 Answers

You can attach the context to your exception manually. I have never tried it, but it would be interesting to create a custom exception that in the constructor calls and saves get_defined_vars() for later retrieval.
This will be a heavy exception :-)

proof of concept:

class MyException extends Exception()  {
    protected $throwState;

    function __construct()   {
        $this->throwState = get_defined_vars();
        parent::__construct();
    }

    function getState()   {
        return $this->throwState;
    }
}

even better:

class MyException extends Exception implements IStatefullException()  {
    protected $throwState;

    function __construct()   {
        $this->throwState = get_defined_vars();
        parent::__construct();
    }

    function getState()   {
        return $this->throwState;
    }

    function setState($state)   {
        $this->throwState = $state;
        return $this;
    }
}

interface  IStatefullException { function getState(); 
      function setState(array $state); }


$exception = new MyException();
throw $exception->setState(get_defined_vars());
like image 90
Exception e Avatar answered Nov 07 '22 03:11

Exception e


Couldn't you also do:

class ContextException extends Exception {

    public $context;

    public function __construct($message = null, $code = 0, Exception $previous = null, $context=null) {
        parent::__construct($message, $code, $previous);
        $this->context = $context;
    }

    public function getContext() {
        return $this->context;
    }
}

That would avoid the need to instantiate the exception and then throw it.

like image 21
Andrew Avatar answered Nov 07 '22 03:11

Andrew


Exceptions in PHP:

http://www.php.net/manual/en/language.exceptions.extending.php

Methods of the basic Exception class:

final public  function getMessage();        // message of exception
final public  function getCode();           // code of exception
final public  function getFile();           // source filename
final public  function getLine();           // source line
final public  function getTrace();          // an array of the backtrace()
final public  function getPrevious();       // previous exception
final public  function getTraceAsString();  // formatted string of trace

So, this is what you have to work with if you caught a basic exception. If you don't have control over the code that generates the exception then there's not much to be done about getting any more context as the context in which it was thrown is gone by the time you catch it. If you are generating the exception yourself then you can attach the context to the exception before it's thrown.

like image 35
Nate C-K Avatar answered Nov 07 '22 04:11

Nate C-K