8

According the PHP Manual: Internal PHP functions mainly use Error reporting, only modern Object oriented extensions use exceptions. However, errors can be simply translated to exceptions with ErrorException

The example provided in ErrorException:

<?php
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");

It seems to allow using Exceptions instead of the default error reporting. My question is, is this an encouragement or merely an option for us?

Moreover, which is a better practice, use Exception alone like the above example, or use both Exception (set_exception_handler) and Error reporting (set_error_handler) alongside with each other?

Community
  • 1
  • 1
IMB
  • 15,163
  • 19
  • 82
  • 140

2 Answers2

12

Short answer: No. That are two different functions.

Long answer: It's not meant to replace but to make use of. set_exception_handlerDocs is used for exceptions and set_error_handlerDocs for errors. That are two different pair of shoes.

See as well:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    The answer/example of Kris in http://stackoverflow.com/questions/841500/php-exceptions-vs-errors seems like my example too. It appears that he also combining errors and exceptions in to one Exception handler, therefore "replacing" the classic behavior of Error reporting? – IMB May 28 '12 at 13:36
  • 1
    Yes that is possible by making use of the built-in `ErrorException` class, see as well the examples in the PHP manual. Take care that much of errors vs. exceptions is subjective. You need to decide for your code/app which methodology you want to follow/mix/use. The key point of my answer is, that the two functions are two type of tools, two pair of shoes, but both used for walking (here error and exception handling which is in the same domain). – hakre May 28 '12 at 13:39
  • So I guess it's perfectly safe to use Exception as a mechanism to handle both Exceptions and Errors, instead of using a different handler for Errors and a different handler for Exceptions? – IMB May 28 '12 at 13:42
  • 1
    There are some errors that need a special treatment that is to my knowledge not possible with exceptions and that are catchable fatal errors. However if you ask me personally, I often prefer Exception driven programming than error handling because you can't go around Exceptions ;) So the code often is more strict. – hakre May 28 '12 at 13:46
  • so annoying. i hate php. they don't even offer a common abstract one that encompasses both errors and exceptions. – ahnbizcad Nov 04 '20 at 00:49
  • @ahnbizcad: See my previous comment, it's not possible to treat both the same, barking up the wrong tree. Such a common abstraction that encompasses both errors and exception risks to be inherently flawed. Nevertheless, the PHP manual on ErrorException has an example on how you can streamline: ***Example #1 Use set_error_handler() to change error messages into ErrorException***. And last but not least mind the error hierarchy since PHP 7 which offers a more abstract model over errors and abstractions: [Errors in PHP 7](https://www.php.net/manual/en/language.errors.php7.php) in the runtime. – hakre Nov 04 '20 at 15:57
0

No, Any exception that is not caught results in a fatal error. If you want to respond gracefully to exceptions that are not caught in catch blocks, then you’ll need to set a function as the default exception handler. To do so, you use the set_exception_handler() function, which accepts a callable as its parameter. Your script will terminate after the callable has executed. The function restore_exception_handler() will revert the exception handler to its previous value.

Alireza Rahmani Khalili
  • 2,727
  • 2
  • 32
  • 33