Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Converting all Errors to Exceptions - Good or Bad?

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:

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.

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

like image 936
anonymous coward Avatar asked Jun 10 '09 16:06

anonymous coward


People also ask

Is it bad to catch all exceptions?

Because when you catch exception you're supposed to handle it properly. And you cannot expect to handle all kind of exceptions in your code. Also when you catch all exceptions, you may get an exception that cannot deal with and prevent code that is upper in the stack to handle it properly.

Why is it bad to throw exceptions?

Exception in the throws clause. But that doesn't mean that you should do that. Specifying an Exception or Throwable makes it almost impossible to handle them properly when calling your method. The only information the caller of your method gets is that something might go wrong.

Should we always use exceptions?

As a general rule of thumb, throw an exception when your program can identify an external problem that prevents execution. If you receive data from the server and that data is invalid, throw an exception.

Does exception catch all exceptions PHP?

Catching all PHP exceptions Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... }


2 Answers

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.

like image 84
Jet Avatar answered Oct 12 '22 08:10

Jet


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.

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

like image 42
Ozzy Avatar answered Oct 12 '22 08:10

Ozzy