Try / catch block in PHP does not catch Exception

I am trying to run this example # 1 from this page: http://php.net/manual/en/language.exceptions.php

<?php function inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } return 1/$x; } try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } // Continue execution echo "Hello World\n"; ?> 

However, instead of the desired output, I get:

 0.2 Fatal error: Uncaught exception 'Exception' with message 'Division by zero.' in xxx: 7 Stack trace: #0 xxx(14): inverse(0) #1 {main} thrown in xxx on line 7 

I am using UniServer 3.5 developer environment with PHP 5.2.3

+81
php
Jan 31 '10 at 18:09
source share
10 answers

I just had this exact problem when it seemed to me that I even copied the name of the exception, but still did not catch it. It turned out that it was my stupid mistake, but I thought that I should publish my case here if there is someone else in the same situation.

I had my exception in my namespace A, and the script in the namespace called B. The problem was that I had A \ MyException, which equals (in PHP) \ B \ A \ MyException (because my the script is in the namespace B!). All I had to do to fix this was to add a backslash (or something else) to the exception name so that it looked like this: \ A \ MyException

+194
Oct. 25 '11 at 16:56
source share

Pretty old question, but ...

I also had this problem (and that I found this post), but a simple experiment allowed me to find a solution. Just try changing Exception to \Exception . Worked for me!

EDIT:

As Sivann pointed out in the comments, using a namespace should do the same. So just type use \Exception as Exception; before class declaration.

+48
Dec 10 '15 at 19:35
source share

Try putting catch(\Exception $e) instead of catch(Exception $e) . If you use code that you don’t know about it very well, or, especially, if you use the framework, it can override the default PHP Exception with one of its own, and therefore you can go the wrong way and get an undesirable result. If you just put \Exception , then you are sure that you will catch the basic PHP exception.

+25
Apr 20 '17 at 8:52
source share

You cannot use typical try {} catch {} blocks in PHP, as you could do in another language such as C # (Csharp).

If you do this:

 try{ //division by zero $number = 5/0; } catch(Exception $ex){ echo 'Got it!'; } 

You will not see the "Got" message never. What for? This is simply because PHP always needs an exception to be thrown. You need to install your own error handler and throw an exception with it.

See the set_error_handler function: http://php.net/manual/es/function.set-error-handler.php

+20
Oct 29 2018-11-11T00:
source share

The identical code does not work for you, otherwise the exception will be selected on line 4 instead of 7. Which code do you use exactly?

+5
Jan 31 '10 at 18:12
source share

My starting point, although you have a typo in the name of the exception you catch / throw, but if your code is exactly the same, I don’t know exactly what is happening.

Try the following modification of the original script and paste your results. This will help to diagnose your problem a little 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'; ?> 
+5
Jan 31 '10 at 23:11
source share

I had the same problem with the following configurations,

PHP 5.2.14 (cli) (built: August 12, 2010 17:32:30) Copyright (c) 1997-2010 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 disable eAccelerator or upgrade it. I tried both and both fixes worked. The error is reported here https://eaccelerator.net/ticket/242 (NB. Firefox complains about their SSL certificate).

Now I run try catch correctly with the following configurations,

PHP 5.2.4 (cli) (built: 10/16/2007 09:13:35) Copyright (c) 1997-2007 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

+4
Dec 20 '10 at 8:17
source share

in Xdebug there is a parameter:

 xdebug.show_exception_trace = 1 

This will force php to throw exceptions even in the catch try block. Turn it to 0

+3
Nov 03 '13 at 16:48
source share

Perhaps try disabling some third-party extensions that you might have installed? http://bugs.php.net/bug.php?id=41744

+1
Feb 01 2018-10-01T00
source share

\ Exception does not work for me, but I found a solution.

I needed to replace try {} catch (Exception $ e) {} with try {} catch (Throwable $ e) {}.

For more information: https://trowski.com/2015/06/24/throwable-exceptions-and-errors-in-php7/

0
Jun 20 '19 at 9:31
source share



All Articles