Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception handling in PHP: where does $e goes?

Tags:

exception

php

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

   try
    {
        //some code

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

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

like image 552
jcslzr Avatar asked Jun 05 '09 17:06

jcslzr


People also ask

How to handle exceptions in PHP?

Exception Handling in PHP is almost similar to exception handling in all programming languages. PHP provides following specialized keywords for this purpose. try: It represent block of code in which exception can arise.

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 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 the advantages of using try catch in PHP?

With try Catch block code becomes readable. Grouping of error types: In PHP both basic types and objects can be thrown as exception. It can create a hierarchy of exception objects, group exceptions in namespaces or classes, categorize them according to types.


4 Answers

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

Here's a silly 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 />");
}

The output of the above is

I'm the outer exception handler (This is thrown from the inner exception handler.)

like image 80
Mark Biek Avatar answered Sep 18 '22 10:09

Mark Biek


One nice thing is that Exception implements __toString() and outputs a call stack trace.

So sometimes in low-level Exceptions that I know I'm gonna want to see how I got to, in the catch() I simply do

error_log($e);
like image 37
grantwparks Avatar answered Sep 20 '22 10:09

grantwparks


$e is an instance of Exception or any other class that extended from Exception. Those objects have some specific attributes and methods in common (inherited from the Exception class) you can use. See the chapter about exceptions and the Exception member list for more details.

like image 24
Gumbo Avatar answered Sep 19 '22 10:09

Gumbo


I'm assuming your using some sort of third party code/library with this code in it that is throwing the exception into your code. You simply have to be ready for an exception to be thrown to catch it, then you can log it/display it however you want.

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

For more information read the PHP manual's entry on Exceptions.

like image 34
TJ L Avatar answered Sep 20 '22 10:09

TJ L