PHP exception handling: where does $ e go?

I was looking for this, and I just seem to run into the same articles in this code:

   try
    {
        //some code

    }
    catch(Exception $e){
        throw $e;
    }

Where is $ e stored or how does the webmaster see it? Should I look for a special function?

+5
source share
5 answers

An An exception object (in this case $ e) thrown from inside the catch {} block will be caught by the next highest attempt {} catch {} block.

Here is a stupid example:

try {
    try {
        throw new Exception("This is thrown from the inner exception handler.");
    }catch(Exception $e) {
        throw $e;
    }
}catch(Exception $e) {
    die("I'm the outer exception handler (" . $e->getMessage() . ")<br />");
}

Above result

I am an external exception handler (this is called from the internal exception handler).

+7
source

It's good that Exception implements __toString () and prints a call stack trace.

, , , , catch()

error_log($e);
+5

$e - Exception , Exception. ( Exception), . . .

+1

, - /​​ , . , , , /, .

try {
  $Library->procedure();
catch(Exception $e) {
  echo $e->getMessage(); //would echo the exception message.
}

PHP Exceptions.

+1
source

Rows:

catch(Exception $e){
  throw $e;
}

It makes no sense. When you catch the Exception, you have to do something with the exception:

catch(Exception $e){
  error_log($e->getMessage());
  die('An error has occurred');
}

But in your case, the Exception is thrown directly into the external try-block that has already occurred.
If you change your code to:

//some code

Would create the same behavior.

+1
source

All Articles