Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Try Catch for Entire Class

Simple question but can't seem to find the answer.

If I have a php class, is it possible to register an exception handler for the whole class?

The reason I want to do this is that my class uses objects that are part of my domain model. These object's methods throw very explicit exceptions. I don't want these exceptions to bubble up to higher level classes, but instead want to catch all these exceptions and throw them as a more general exception e.g. DomainLayerException

I would therefore like one area in my class that catches any number of a list of exceptions I define from my domain model, and throws them as a more general exception e.g.

Currently the way I do this is by wrapping method calls to the domain objects in a try catch block. This is getting very messy as I use more and more domain objects and their methods. Would be great to remove these try catch blocks and handle them all on one place in the class i.e. if any exception is thrown in the class, it is caught by a single event handler defined in the class

like image 679
GWed Avatar asked Jul 03 '14 11:07

GWed


People also ask

Is there a try catch in PHP?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

Does try catch stop execution PHP?

Try catch in PHP is a block that contains the program's vital code for execution. In PHP, the finally block is also used to clean up the code. The only difference is that it always runs regardless of whether or not an exception is thrown.

Which is the correct way to use catch block in PHP?

The “try” block is executed and an exception is thrown if the denominator is zero or negative number. The “catch” block catches the exception and displays the error message. The flowchart below summarizes how our sample code above works for custom types of exceptions.

What is exceptional handling in PHP?

Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception. This is what normally happens when an exception is triggered: The current code state is saved.


1 Answers

You could use a proxy class to perform the calls on your behalf and let you wrap the exception:

class GenericProxy
{
    private $obj;
    private $handler;

    public function __construct($target, callable $exceptionHandler = null)
    {
        $this->obj = $target;
        $this->handler = $exceptionHandler;
    }

    public function __call($method, $arguments)
    {
        try {
            return call_user_func_array([$this->obj, $method], $arguments);
        } catch (Exception $e) {
            // catch all
            if ($this->handler) {
                throw call_user_func($this->handler, $e);
            } else {
                throw $e;
            }
        }
    }
}

$proxy = new GenericProxy($something, function(Exception $e) {
    return new MyCoolException($e->getMessage(), $e->getCode(), $e);
});
echo $proxy->whatever_method($foo, $bar);

It uses the __call() magic method to intercept and forward method calls to the target.

like image 124
Ja͢ck Avatar answered Oct 07 '22 23:10

Ja͢ck