在 PHP Try Catch 块中抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9041173/
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
Throwing exceptions in a PHP Try Catch block
提问by kaspnord
I have a PHP function in a Drupal 6 .module file. I am attempting to run initial variable validations prior to executing more intensive tasks (such as database queries). In C#, I used to implement IF statements at the beginning of my Try block that threw new exceptions if a validation failed. The thrown exception would be caught in the Catch block. The following is my PHP code:
我在 Drupal 6 .module 文件中有一个 PHP 函数。我试图在执行更密集的任务(例如数据库查询)之前运行初始变量验证。在 C# 中,我曾经在 Try 块的开头实现 IF 语句,如果验证失败,则会抛出新的异常。抛出的异常将在 Catch 块中捕获。以下是我的PHP代码:
function _modulename_getData($field, $table) {
try {
if (empty($field)) {
throw new Exception("The field is undefined.");
}
// rest of code here...
}
catch (Exception $e) {
throw $e->getMessage();
}
}
However, when I try to run the code, it's telling me that objects can only be thrown within the Catch block.
但是,当我尝试运行代码时,它告诉我只能在 Catch 块中抛出对象。
Thanks in advance!
提前致谢!
回答by AlienWebguy
function _modulename_getData($field, $table) {
try {
if (empty($field)) {
throw new Exception("The field is undefined.");
}
// rest of code here...
}
catch (Exception $e) {
/*
Here you can either echo the exception message like:
echo $e->getMessage();
Or you can throw the Exception Object $e like:
throw $e;
*/
}
}
回答by Chen Kinnrot
To rethrow do
重新抛出做
throw $e;
not the message.
不是消息。
回答by DaveRandom
Just remove the throw
from the catch block — change it to an echo
or otherwise handle the error.
只需throw
从 catch 块中删除- 将其更改为 anecho
或以其他方式处理错误。
It's not telling you that objects can only be thrown in the catch block, it's telling you that only objectscan be thrown, and the location of the error is in the catch block — there is a difference.
它不是告诉您只能在 catch 块中抛出对象,而是告诉您只能在 catch 块中抛出对象,并且错误的位置在 catch 块中——这是有区别的。
In the catch block you are trying to throw something you just caught — which in this context makes little sense anyway — and the thing you are trying to throw is a string.
在 catch 块中,你试图抛出你刚刚捕获的东西——无论如何在这种情况下这没什么意义——你试图抛出的东西是一个字符串。
A real-world analogy of what you are doing is catching a ball, then trying to throw just the manufacturer's logo somewhere else. You can only throw a whole object, not a property of the object.
您正在做的现实世界的类比是接球,然后试图将制造商的标志扔到其他地方。您只能抛出整个对象,而不能抛出对象的属性。
回答by KingCrunch
throw $e->getMessage();
You try to throw a string
你尝试抛出一个 string
As a sidenote: Exceptions are usually to define exceptional states of the application and not for error messages after validation. Its not an exception, when a user gives you invalid data
作为旁注:异常通常用于定义应用程序的异常状态,而不是用于验证后的错误消息。当用户向您提供无效数据时,这也不例外
回答by Goms
Throw needs an object instantiated by \Exception
. Just the $e
catched can play the trick.
Throw 需要一个由 实例化的对象\Exception
。只有被$e
抓住的人才能发挥作用。
throw $e