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/
php exception throw
darksoulsong
source share