Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using additional data in php exceptions

I have php code that execute python cgi and I want to pass python trace (returned from cgi) as extra data to php exception how can I do this and how can I get that value from catch(Exception e) { (It should check if that extra value exesit or not).

I have code like this:

$response = json_decode(curl_exec($ch));
if (isset($response->error)) {
    // how to send $response->trace with exception.
    throw new Exception($response->error);
}
return $response->result;

and I use json-rpc library that should return that data to the user:

} catch (Exception $e) {
    //catch all exeption from user code
    $msg = $e->getMessage();
    echo response(null, $id, array("code"=>200, "message"=>$msg));
}

Do I need to write new type of exception or can I do this with normal Exception? I would like to send everything that was thrown in "data" =>

like image 810
jcubic Avatar asked Mar 01 '14 11:03

jcubic


People also ask

What is an exception how you can handle multiple exceptions in PHP?

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.

What are the 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.

Can we use multiple catch blocks in PHP?

Multiple catch blocks can be used to catch different classes of exceptions. Normal execution (when no exception is thrown within the try block) will continue after that last catch block defined in sequence. Exceptions can be throw n (or re-thrown) within a catch block.

How to create custom exception class in PHP?

The class must be an extension of the exception class. The custom exception class inherits the properties from PHP's exception class and you can add custom functions to it. Lets create an exception class: The new class is a copy of the old exception class with an addition of the errorMessage () function.

What are exceptions in PHP and how to handle them?

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 the variable name for a caught exception in PHP?

As of PHP 8.0.0, the variable name for a caught exception is optional. If not specified, the catch block will still execute but will not have access to the thrown object. A finally block may also be specified after or instead of catch blocks.

How to add custom functions to a custom exception class?

The custom exception class inherits the properties from PHP's exception class and you can add custom functions to it. Lets create an exception class: The new class is a copy of the old exception class with an addition of the errorMessage () function.


1 Answers

You need to extend Exception class:

class ResponseException extends Exception 
{
    private $_data = '';

    public function __construct($message, $data) 
    {
        $this->_data = $data;
        parent::__construct($message);
    }

    public function getData()
    {
        return $this->_data;
    }
}

When throwing:

...
throw new ResponseException($response->error, $someData);
...

And when catching:

catch(ResponseException $e) {
    ...
    $data = $e->getData();
    ...
}

UPDATE - DYNAMIC OBJECT PROPERTY (aka DIRTY WAY)

As the OP asking about doing this task without extending Exception class, you can totally skip ResponseException class declaration. I really not recommend do it this way, unless you've got really strong reason (see this topic for more details: https://softwareengineering.stackexchange.com/questions/186439/is-declaring-fields-on-classes-actually-harmful-in-php)

In throwing section:

...
$e = new Exception('Exception message');
$e->data = $customData; // we're creating object property on the fly
throw $e;
...

and when catching:

catch(Exception $e) {
    $data = $e->data; // Access data property
}

September 2018 edit: As some of readers found this answer useful, I have added a link to another Stack Overflow question which explains the downsides of using dynamically declared properties.

like image 154
barell Avatar answered Oct 16 '22 05:10

barell