views:

26

answers:

1

Hi everyone,

I'm currently working on an application that requires lots of external libraries. My job right now is set a unique error handler that will manage every error.

So far, I found 7 different types of PEAR errors:

  • PEAR_ERROR_RETURN:
  • PEAR_ERROR_EXCEPTION:
  • PEAR_ERROR_CALLBACK:
  • PEAR_ERROR_PRINT:
  • PEAR_ERROR_TRIGGER:
  • PEAR_ERROR_DIE:

I want to handle only the serious error (like the native E*_ERROR). The only problem is I have absolutely no idea about PEAR error criticity ! Those names are more related to the way of handling them than to their seriousness... Is there a real documentation about it ?

I guess a better solution would consist in using their pushErrorHandling, but I just don't understand how to use it... If someone here knows, I'd be grateful...

Thanks in advance !

A: 

From your question it sounds like you're wanting a single function to handle all warnings, errors, and exceptions.

For E_ERROR, I think you're out of luck. That indicates a fatal runtime error that PHP cannot recover from, so your script is doomed to fail.

With exceptions you can do a set_exception_handler("funcName"); to handle all uncaught exceptions in your program execution. However, this should be an absolute last resort for handling exceptions. A safety net, if you will, for anything that slips through the cracks of your try/catches. The reason for this (and for handling any error globally like this) is that you have no way to recover from the exception since set_exception_handler doesn't return a value and it has exited the program stack anyway.

Use try/catch generously in your application. It may seem tedious, or a waste of time, but the truth is that your program will fail at some point. It is inevitable, no matter how good a programmer you are. Proper use of exception handling will allow your application to gracefully close, or even recover properly from an exception.

Jarrod
Hi and thanks for your reply, indeed, I must manage to centralize the error/exception handling... I am well aware exception is the most proper solution, however I simply cannot change all external code that rely on old error handling (like the version of pear I have)...
Rolf