user1544337
user1544337

Reputation:

Separate exception handler for PDO

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?

Upvotes: 0

Views: 265

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157989

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.

Upvotes: 2

Ven
Ven

Reputation: 19040

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);
});

Upvotes: 4

Related Questions