Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is set_exception_handler meant to replace set_error_handler?

Tags:

php

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?

like image 268
IMB Avatar asked May 28 '12 12:05

IMB


1 Answers

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:

  • PHP: exceptions vs errors?
  • PHP: What is the flow of control for error handling?
  • Error Handling and LoggingDocs
like image 50
hakre Avatar answered Nov 03 '22 01:11

hakre