tags:

views:

403

answers:

5

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?

+5  A: 

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.)

Mark Biek
Thanks Mark, and excuse my newbiness but how do I check getMessage so I see what errors are coming up?
Slzr
$e->getMessage() is just a function that returns a string with the error message.Example #1 here (http://www.php.net/manual/en/language.exceptions.extending.php) has some good info about what methods are available in a standard Exception object.
Mark Biek
Thank you, I will check itCS
Slzr
+1  A: 

$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.

Gumbo
+1  A: 

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.

tj111
+1  A: 

The lines:

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

Don\t make sense. When you catch an Exception you're suppose to do something with the exception like:

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

But in your case the Exception is thrown directly to an outer try-block which would already happen.
If you change your code to:

//some code

Would create the exact same behaviour.

Bob Fanger
Yes! I hate seeing code where an Exception is caught and only re-thrown. Exception handling is not given the attention it should, which results in random error handling in a large system instead of clean code and easy to diagnose problems.
grantwparks
+4  A: 

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);
grantwparks
thank you for this, the docs don't show passing an Exception object to error_log(). I thought I was searching way to long for this functionality. catch it, handle it, and log it by the default handler, didn't seem hard till you try and find it.
ddowns
You're very welcome. I basically stumbled on the stack trace aspect of Exception's `__toString()` implementation and used it to replace a legacy function we were using.
grantwparks