PHP Web 应用程序何时使用 try/catch
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20670680/
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 web application when to use try/catch
提问by Edward
I am using PHP with CodeIgniter framework. I read some articles that stating using try/catch methods is in bad practice.
我正在使用带有 CodeIgniter 框架的 PHP。我读过一些文章,指出使用 try/catch 方法是不好的做法。
I understand to use logging in development when a potential error can occur and let the actual error happen then log it using log_message('level', 'message')
我了解在可能发生潜在错误时使用登录开发,让实际错误发生,然后使用 log_message('level', 'message')
But when in deployment you want to suppress and handle any errors that arise. Should I be using try/catch blocks, for instance in something like...
但是在部署时,您希望抑制和处理出现的任何错误。我应该使用 try/catch 块,例如在类似...
try {
$this->data['important'] = $this->Test_Model->do_something($data);
if(empty(data['important'])) {
throw new Exception('no data returned');
}
catch (Exception $e) {
//alert the user then kill the process
var_dump($e->getMessage());
}
I'm confused when I use be using try/catch correctly.
当我正确使用 try/catch 时,我很困惑。
回答by CentaurWarchief
There's a syntax error in your code, it should be:
您的代码中有语法错误,应该是:
try {
$this->data['important'] = $this->Test_Model->do_something($data);
if(empty($this->data['important'])) {
throw new Exception('no data returned');
}
} catch (Exception $e) {
//alert the user.
var_dump($e->getMessage());
}
Using try/catchisn't a bad practice.
使用try/catch不是一个坏习惯。
Exceptions are catch-able without needing to override PHP default error handler. This means that you can do something when exceptions occurs in run-time.
异常是可以捕获的,无需覆盖 PHP 默认错误处理程序。这意味着您可以在运行时发生异常时做一些事情。
Logs are for you. If your application isn't healthy and you logged possible problematic points, you can identify errors more easily. The main difference between your log and native log is the context, native log doesn't have context, they have just the occurrence and possibly stack trace.
日志是给你的。如果您的应用程序不健康并且您记录了可能的问题点,您可以更轻松地识别错误。您的日志和本机日志之间的主要区别在于上下文,本机日志没有上下文,它们只有发生和可能的堆栈跟踪。
回答by Sammitch
Exceptions should only be caught in two cases:
异常应该只在两种情况下被捕获:
- When the exception itself does not require an immediate halt to your program's execution, ie: the catch block intelligently handles the exception and allows program execution following the block to happen in a sanemanner.
When you want to perform additional error handling and shut down cleanly, and/or log/display error information. Optionally, the exception can then be re-thrown the exception to "bubble up" in the stack so other routines can do the same. eg:
try { //code } catch( Exception $e ) { echo "friendly error message"; logging_function($e->getMessage()); throw $e; }
- 当异常本身不需要立即停止程序的执行时,即:catch 块智能地处理异常并允许程序在块之后以理智的方式执行。
当您想要执行额外的错误处理和干净地关闭,和/或记录/显示错误信息时。可选地,然后可以重新抛出异常以在堆栈中“冒泡”,以便其他例程可以执行相同的操作。例如:
try { //code } catch( Exception $e ) { echo "friendly error message"; logging_function($e->getMessage()); throw $e; }
The most widely-abused method is to simply use a try/catch block to silently ignore exceptions and continue execution as if everything was still OK. Doing something like this will come back and bite you in the ass when something goes wrong muchlater in your program's execution, that that ignored exception would have warned you about, which you then spend hours of your life tracking back to:
最广泛滥用的方法是简单地使用 try/catch 块来静默忽略异常并继续执行,就好像一切都还好一样。做这样的事情会回来咬你的屁股当不顺心的事太多以后在你的程序的执行,这是忽略异常会有关警告你,你再度过自己的一生跟踪回的时间:
try {
// code
} catch( Exception $e ) { /* L7: Exception handling is for squares! */ }
In these cases it's much better to let the Exception raise an error where it happenedinstead of where ignoring that error caused a worse one.
在这些情况下,最好让 Exception在它发生的地方引发错误,而不是忽略该错误导致更糟的错误。

