PHP致命错误:未捕获异常“异常” [英] PHP Fatal error: Uncaught exception 'Exception'
问题描述
if(file_exists($ _SERVER ['DOCUMENT_ROOT'] 。'/'。$ _ GET ['image'])){
//这里真的很奇怪。
}
else {
throw new Exception(请求的文件不存在);
}
问题是,当我尝试提供一个不存在的文件测试,我有一个500错误,而不是异常消息。服务器日志如下:
[09-Jul-2013 18:26:16 UTC] PHP致命错误:未捕获的异常异常与消息'请求的文件不存在'在C:\sites\wonderfulproject\script.php:40
堆栈跟踪:
#0 {主}
扔在C:\sites\wonderfulproject\script.php行40
我想知道我在这里缺少一些真正的东西。
我已经检查了这个问题 PHP致命错误:未捕获异常异常与消息,但它不像我的问题,没有简明扼要的答案。
帮助?
*编辑*
似乎这是与 throw
关键字相关的内容。例如,如果我使用 echo
,我会在屏幕上显示消息,如下所示:
exception '在C:\sites\wonderfulproject\script.php:183堆栈跟踪:#0 {main}
消息'该文件不存在'为什么是这样?
**编辑2 **
感谢@Orangepill,我有了更好的了解关于如何处理异常。我从nettuts找到了一个很棒的tut,帮助了很多。链接: http:// net。 tutsplus.com/tutorials/php/the-ins-and-outs-of-php-exceptions/
这是display_errors关闭的未捕获异常的预期行为。
您的选项是通过php或ini文件打开display_errors或捕获并输出异常。
ini_set(display_errors,1);
或
try {
//可能抛出异常的代码
} catch(异常$ e){
echo $ e-> getMessage();
}
如果你抛出异常,意图是,将抓住并处理它。如果不是,则是服务器错误(500)。
另一个选项是使用 set_exception_handler 设置脚本的默认错误处理程序。
function default_exception_handler(Exception $ e){
//向用户显示某些内容,让他们知道我们跌倒
echo< h2>发生不良事件< / h2>;
echo< p>我们填写查找负责人并让他们拍摄< / p>;
//为异常做一些日志记录,并调用kill_programmer函数。
}
set_exception_handler(default_exception_handler);
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/
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");
这篇关于PHP致命错误:未捕获异常“异常”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!