PHP - 将所有错误转换为异常 - 好还是坏?

时间:2022-05-23 22:03:15

I was wondering if it's considered a bad practice to globally convert all PHP Errors to Exceptions. Something like the following would be used:

我想知道将所有PHP错误全局转换为异常是否被认为是一种不好的做法。将使用以下内容:

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
    return false;
}

I suppose the assumption is that you can just start using "try/catch" around certain pieces of code that would normally throw Errors.

我想假设您可以开始在某些通常会引发错误的代码片段周围使用“try / catch”。

If it's not a case of Good/Bad, what are some of the Gotchas that could arise from this practice?

如果不是好/坏的情况,那么这种做法会产生什么样的问题呢?

3 个解决方案

#1


8  

Unfortunately, this won't work on fatal/parse/etc. errors...

不幸的是,这不适用于致命/解析/等。错误...

Don't remember exactly, but I've tried this and in some cases got a message like "can't throw exception without workaround..." but I can't remember the conditions to get this result. But now I use this way and completely satisfied.

不记得确切,但我已经尝试过这种情况,并且在某些情况下得到了一条消息,例如“不能在没有解决方法的情况下抛出异常......”但是我不记得获得此结果的条件。但现在我用这种方式完全满意。

#2


2  

Use exceptions for things that are truly beyond your control.

对真正无法控制的事物使用例外。

Good:

try {
   if (fopen('file.txt', 'w') === false) {
      throw new Exception('File could not be opened for write access.');
   }
} catch (Exception $e) {
   echo $e->getMessage();
}

Bad:

try {
   if (strlen($_POST['username']) < 5) {
      throw new Exception('Username too short');
   }
} catch (Exception $e) {
   echo $e->getMessage();
}

The first way is good because it occurs when its something that the user or the application cant control. It cant open the file because? could be many reasons.

第一种方法是好的,因为它发生在用户或应用程序无法控制的东西时。它无法打开文件,因为?可能有很多原因。

The second way is an overkill use of try /catch when you should use trigger_error. The second way is down to the user not knowing the rules of the username validation.

第二种方法是当你应该使用trigger_error时,尝试使用try / catch。第二种方式是用户不知道用户名验证的规则。

In short use exceptions when you cant control what your testing. Remember exceptions have more overhead then trigger_error aswell :)

简而言之,当你无法控制你的测试时,会使用异常。记住异常有更多的开销然后trigger_error以及:)

#3


1  

having worked many years in Java/.Net in the past and now php in recent years, it's really annoying to have all these various error conventions, while the Exceptions Pattern is really good for everything - starting from application level errors, class errors, to system errors - anything.

在过去的Java / .Net工作多年,现在已经在PHP工作了很多年,拥有所有这些不同的错误约定真的很烦人,而Exceptions Pattern对所有事情都非常好 - 从应用程序级错误,类错误到系统错误 - 任何事情。

I really invest quite a lot of work in trying to manage all sort of errors types, because each library/functions handles errors differently.

我真的投入了大量的工作来尝试管理所有类型的错误,因为每个库/函数处理错误的方式不同。

#1


8  

Unfortunately, this won't work on fatal/parse/etc. errors...

不幸的是,这不适用于致命/解析/等。错误...

Don't remember exactly, but I've tried this and in some cases got a message like "can't throw exception without workaround..." but I can't remember the conditions to get this result. But now I use this way and completely satisfied.

不记得确切,但我已经尝试过这种情况,并且在某些情况下得到了一条消息,例如“不能在没有解决方法的情况下抛出异常......”但是我不记得获得此结果的条件。但现在我用这种方式完全满意。

#2


2  

Use exceptions for things that are truly beyond your control.

对真正无法控制的事物使用例外。

Good:

try {
   if (fopen('file.txt', 'w') === false) {
      throw new Exception('File could not be opened for write access.');
   }
} catch (Exception $e) {
   echo $e->getMessage();
}

Bad:

try {
   if (strlen($_POST['username']) < 5) {
      throw new Exception('Username too short');
   }
} catch (Exception $e) {
   echo $e->getMessage();
}

The first way is good because it occurs when its something that the user or the application cant control. It cant open the file because? could be many reasons.

第一种方法是好的,因为它发生在用户或应用程序无法控制的东西时。它无法打开文件,因为?可能有很多原因。

The second way is an overkill use of try /catch when you should use trigger_error. The second way is down to the user not knowing the rules of the username validation.

第二种方法是当你应该使用trigger_error时,尝试使用try / catch。第二种方式是用户不知道用户名验证的规则。

In short use exceptions when you cant control what your testing. Remember exceptions have more overhead then trigger_error aswell :)

简而言之,当你无法控制你的测试时,会使用异常。记住异常有更多的开销然后trigger_error以及:)

#3


1  

having worked many years in Java/.Net in the past and now php in recent years, it's really annoying to have all these various error conventions, while the Exceptions Pattern is really good for everything - starting from application level errors, class errors, to system errors - anything.

在过去的Java / .Net工作多年,现在已经在PHP工作了很多年,拥有所有这些不同的错误约定真的很烦人,而Exceptions Pattern对所有事情都非常好 - 从应用程序级错误,类错误到系统错误 - 任何事情。

I really invest quite a lot of work in trying to manage all sort of errors types, because each library/functions handles errors differently.

我真的投入了大量的工作来尝试管理所有类型的错误,因为每个库/函数处理错误的方式不同。