Separate exception handler for PDO

一世执手 提交于 2019-12-20 06:25:17

问题


In my PHP code, I have several types of exceptions. One is a 'normal' Exception, another is PDOException. I'm using set_exception_handler($handler) to catch exceptions automatically.

Is there any way I can get separate handlers for Exception and PDOException?
If not, can I check the type of the exception in the handler?


回答1:


I think you should have a "global" handler, and branch.

set_exception_handler(function ($exception) {
  if ($exception instanceof PDOException) {
    handle_pdo_exception($exception);
    return;
  }
  log($exception);
});



回答2:


Thank you for clarifying your question.

I think you are asking out of wrong assumption.

As a matter of fact, either normal exception needs to be logged to a common error_log and PDOexception have to trigger a generic 500 error page shown to user. There is no point in separating these matters. So, you can use common exception handler to handle all exceptional events in your code.



来源:https://stackoverflow.com/questions/16810842/separate-exception-handler-for-pdo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!