Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try/Catch block in PHP not catching Exception

Tags:

php

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 

The developer environment I am using is UniServer 3.5 with PHP 5.2.3

like image 299
Krassi Avatar asked Jan 31 '10 18:01

Krassi


People also ask

What happens if exception is not caught in try catch?

If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console. The message typically includes: name of exception type.

How can I catch exception in PHP?

Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... } catch ( \Exception $e ) { // ... }

Can we use try catch in PHP?

PHP supports using multiple catch blocks within try catch. This allows us to customize our code based on the type of exception that was thrown. This is useful for customizing how you display an error message to a user, or if you should potentially retry something that failed the first time.

Can we use try without catch in PHP?

You must use catch with try . Please look php.net manual. PHP has an exception model similar to that of other programming languages. An exception can be thrown, and caught ("catched") within PHP.


2 Answers

I just had this exact problem where it seemed like I had even copied the name of the exception and yet it didn't catch it. It turned out it was my stupid mistake but I thought I should post my case here in case there is someone else in the same situation.

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

like image 53
Pijusn Avatar answered Nov 15 '22 17:11

Pijusn


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.

like image 30
Enethion Avatar answered Nov 15 '22 16:11

Enethion