PHP Fatal error: Uncaught exception 'Exception'
Asked Answered
D

3

11

I'm playing around with exceptions in PHP. For example, I have a script that reads a $_GET request and loads a file; If the file doesn't exists, an new exception should be thrown:

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 supply an non existent file for the test, I got a 500 error instead of the exception message. The server log is the following:

[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 obvious here.

I've checked this question PHP fatal error: Uncaught exception 'Exception' with message but it's not quite like my issue, and have no concise answer.

Help, please?

* EDIT *

It seems this is something related to the throw keyword. If I use echo for example, I got the message printed on the screen, like this:

exception 'Exception' with message 'The file does not exists.' in C:\sites\wonderfulproject\script.php:183 Stack trace: #0 {main}

Why is that?

** EDIT 2 **

Thanks to @Orangepill, I got a better understanding about how to handle exceptions. And I found a superb tut from nettuts that helped a lot. The link: http://net.tutsplus.com/tutorials/php/the-ins-and-outs-of-php-exceptions/

Deccan answered 9/7, 2013 at 18:49 Comment(4)
Your log tells you exactly what you wanted it to tell, what is the issue?Iow
file_exists obviously returned false, likely because the file does in fact not exist.Machicolation
@DanyCaissy The issue is that I'm getting a 500 - Internal Server Error, instead of having the exception with my message and the stack trace in the screen.Deccan
Please be aware of path traversal attacks!Epanorthosis
A
22

This is expected behavior for an uncaught exception with display_errors off.

Your options here are to turn on display_errors via php or in the ini file or catch and output the exception.

 ini_set("display_errors", 1);

or

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

If you are throwing exceptions, the intention is that somewhere further down the line something will catch and deal with it. If not it is 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");
Allen answered 9/7, 2013 at 18:54 Comment(4)
Nice. But I thought I could throw exceptions outside a try catch block, correct me if I'm wrong, please. I've updated my question with a new description of what I have tried.Deccan
you can... but you will get a 500 server message :).... There is no point in throwing an exception except to give yourself the opportunity to catch and deal with it, or at least format an informative "oops" page for the end user if the exception is not recoverable.Allen
The try/catch doesn't have to be in the function that throws the exception it can also be further down in the execution stack... like in the caller of that function, or that functions' caller.Allen
@Deccan Updated answer to show use of set_exception_handler to create a Pokemon Exception Handler (gotta catch'em all) to handle those exceptions that sneak past your try/catch blocks.Allen
C
10

Just adding a bit of extra information here in case someone 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 the normal PHP error for an uncatched exception was thrown.

Turned out I forgot to add "use \Exception;" at the top, adding that solved the error.

Correlate answered 14/4, 2014 at 22:1 Comment(2)
Or you can define the path in the call to the class: throw new \Exception(...); Note the leading "\" to define the path to the class at the root.Adar
Thanks @Correlate for solving my problem! I was about to get crazy…Catlike
F
0

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 (just for a subfolder). There are also more detailed settings, see Enabling error display in php via htaccess only

Foliaceous answered 5/10, 2017 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.