PHP Fatal Error: Throw Exception Exception

I play with exceptions in PHP. For example, I have a script that reads a $ _GET request and loads a file; If the file does not exist, you must create a new exception:

if ( file_exists( $_SERVER['DOCUMENT_ROOT'] .'/'.$_GET['image'] ) ) { // Something real amazing happens here. } else { throw new Exception("The requested file does not exists."); } 

The problem is that when I try to provide a non-existent file for the test, I received a 500 error instead of an exception message. The server log is as follows:

 [09-Jul-2013 18:26:16 UTC] PHP Fatal error: Uncaught exception 'Exception' with message 'The requested file does not exists.' in C:\sites\wonderfulproject\script.php:40 Stack trace: #0 {main} thrown in C:\sites\wonderfulproject\script.php on line 40 

I wonder if I'm missing something real here.

I checked this question PHP fatal error: Failed to throw an exception "Exception" with the message , but this does not quite look like my problem and does not have a short answer.

Help me please?

* EDIT *

This seems to be related to the throw keyword. If I use echo , for example, I received a message printed on the screen, for example:

exception "Exception" with the message "File does not exist." in C: \ sites \ wonderfulproject \ script.php: 183 Stack Trace: # 0 {main}

Why is this?

** EDIT 2 **

Thanks to @Orangepill, I got a better idea on how to handle exceptions. And I found excellent music from nettuts that helped a lot. Link: http://net.tutsplus.com/tutorials/php/the-ins-and-outs-of-php-exceptions/

+7
php exception throw
source share
3 answers

This is the expected behavior for an uncaught exception with display_errors disabled.

Here you should enable display_errors via php or ini file or catch and throw an exception.

  ini_set("display_errors", 1); 

or

  try{ // code that may throw an exception } catch(Exception $e){ echo $e->getMessage(); } 

If you throw exceptions, the intention is that somewhere further down the line something will catch and handle it. If it is not a server error (500).

Another option for you would be to use set_exception_handler to set a default error handler for your script.

  function default_exception_handler(Exception $e){ // show something to the user letting them know we fell down echo "<h2>Something Bad Happened</h2>"; echo "<p>We fill find the person responsible and have them shot</p>"; // do some logging for the exception and call the kill_programmer function. } set_exception_handler("default_exception_handler"); 
+20
source share

Just add a little extra info here if anyone has the same issue as me.

I use namespaces in my code, and I had a class with a function that throws an exception.

However, my try / catch code in another class file was completely ignored, and a regular PHP error was thrown for an exception thrown.

It turned out that I forgot to add "use \ Exception;" above adding that resolved the error.

+7
source share

For

 throw new Exception('test exception'); 

I got 500 (but didn't see anything in the browser) until I put

 php_flag display_errors on 

in my .htaccess (only for subfolder). There are also more detailed settings, see Enabling error display in php only through htaccess

0
source share

All Articles