PHP 致命错误:未捕获的异常“异常”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17555920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Fatal error: Uncaught exception 'Exception'
提问by darksoulsong
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:
我正在处理 PHP 中的异常。例如,我有一个脚本读取 $_GET 请求并加载文件;如果文件不存在,应该抛出一个新的异常:
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:
问题是,当我尝试为测试提供一个不存在的文件时,我收到了 500 错误而不是异常消息。服务器日志如下:
[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 messagebut it's not quite like my issue, and have no concise answer.
我已经检查了这个问题PHP 致命错误:未捕获的异常 '异常' 与消息,但它不太像我的问题,并且没有简洁的答案。
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:
这似乎与throw
关键字有关。echo
例如,如果我使用,我会在屏幕上打印消息,如下所示:
exception 'Exception' with message 'The file does not exists.' in C:\sites\wonderfulproject\script.php:183 Stack trace: #0 {main}
带有消息“文件不存在”的异常“异常”。在 C:\sites\wonderfulproject\script.php:183 堆栈跟踪:#0 {main}
Why is that?
这是为什么?
** EDIT 2 **
** 编辑 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/
感谢@Orangepill,我对如何处理异常有了更好的理解。我从 nettuts 中找到了一个很棒的 tut,这很有帮助。链接:http: //net.tutsplus.com/tutorials/php/the-ins-and-outs-of-php-exceptions/
回答by Orangepill
This is expected behavior for an uncaught exception with display_errors off.
这是 display_errors 关闭的未捕获异常的预期行为。
Your options here are to turn on display_errors via php or in the ini file or catch and output the exception.
您的选择是通过 php 或在 ini 文件中打开 display_errors 或捕获并输出异常。
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).
如果您抛出异常,其目的是在更远的地方有一些东西会捕获并处理它。如果不是,则是服务器错误 (500)。
Another option for you would be to use set_exception_handlerto set a default error handler for your script.
您的另一个选择是使用set_exception_handler为您的脚本设置默认错误处理程序。
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");
回答by xorinzor
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.
然而,我在另一个类文件中的 try/catch 代码被完全忽略,并且抛出了未捕获异常的正常 PHP 错误。
Turned out I forgot to add "use \Exception;" at the top, adding that solved the error.
原来我忘了添加“use \Exception;” 在顶部,添加解决了错误。
回答by Eugene Lycenok
For
为了
throw new Exception('test exception');
I got 500 (but didn't see anything in the browser), until I put
我得到了 500(但在浏览器中没有看到任何东西),直到我把
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
在我的 .htaccess 中(仅用于子文件夹)。还有更详细的设置,参见Enabling error display in php via htaccess only