I am trying to run this Example #1 from this page: http://php.net/manual/en/language.exceptions.php
I am experiencing this as well. I read comment from Rowinson Gallego which state Exception must be thrown. So I modified my code from :
try
{
$number = 5/0; //or other exception
}
catch(Exception $e)
{
throw $e;
}
into :
try
{
$number = 5/0; //or other exception
}
catch(Exception $e)
{
throw new Exception($e->getMessage(),$e->getCode());
}
It works.
My initial though is you have a typo in the name of the exception you are catching/throwing, but if your code is exactly the same I'm not sure exactly what is going on.
Try the following modification of the original script, and paste your results. It will help diagnose your issue a bit better.
<?php
//set up exception handler to report what we didn't catch
function exception_handler($exception) {
if($exception instanceof MyException) {
echo "you didn't catch a myexception instance\n";
} else if($exception instanceof Exception) {
echo "you didn't catch a exception instance\n";
} else {
echo "uncaught exception of type: ".gettype($exception)."\n";
}
echo "Uncaught exception: " , $exception->getMessage(), "\n";
}
//install the handler
set_exception_handler('exception_handler');
class MyException extends Exception {
}
function inverse($x) {
if (!$x) {
throw new MyException('Division by zero.');
}
else return 1/$x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (MyException $e) {
echo 'Caught myexception: ', $e->getMessage(), "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Continue execution
echo 'Hello World';
?>
in Xdebug there is a setting:
xdebug.show_exception_trace = 1
This will force php to output exceptions even in a try catch block.
Turn this to 0
Quite old question, yet...
I had this problem as well (and that's how I found this post) but just simple experiment allowed me to find the solution. Just try changing Exception
to \Exception
. Worked for me!
EDIT:
As sivann pointed in the comments, using namespace should do the same thing. So simply put use \Exception as Exception;
before your class declaration.
Try to put catch(\Exception $e)
instead of catch(Exception $e)
. If you are using a code you don't know about very well, or - especially - if you are using a framework, it might override the default PHP Exception with one of its own, and therefore you might go to the wrong path and get the undesired result. If you just put \Exception
, then you are sure you are catching the base PHP exception.
I had the same problem with following configurations,
PHP 5.2.14 (cli) (built: Aug 12 2010 17:32:30) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies with eAccelerator v0.9.5.1, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
The solution is to either disable eAccelerator or update it. I tried both and both of the fixes worked. The bug is reported here https://eaccelerator.net/ticket/242 (NB. firefox complains about their SSL cert) .
Now I am running try catch properly with following configurations,
PHP 5.2.4 (cli) (built: Oct 16 2007 09:13:35) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator