11

In the documentation for PHP 7, I noticed that two predefined exceptions, Error and ErrorException are almost exactly the same, with ErrorException having the additional $severity property and Error only being introduced in PHP 7 while ErrorException has existed since PHP 5.1.

From what I understand, Error is the exception which I should use to catch all internal PHP errors, such as type errors, but I don't get what is the purpose of the ErrorException exception. What is the use of each of them, and should I base my custom exceptions off of either of them, or should I stick with the usual Exception?

akukas
  • 555
  • 1
  • 6
  • 11

2 Answers2

9

You can catch purpose of Error class from this page which describes errors in php

PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting mechanism used by PHP 5, most errors are now reported by throwing Error exceptions.

The same description on its own Error page:

Error is the base class for all internal PHP errors.

So you shouldn't use this class for your user defined exceptions.

The purpose of ErrorException you can get from this good SO question/answers:

ErrorException is mostly used to convert php error (raised by error_reporting) to Exception

But in php7 you don't need to convert php error to Exception.

So you basically should extend simple Exception or you can use for standard situations these predefined set of SPL Exceptions (e.g. InvalidArgumentException, OutOfBoundsException, BadFunctionCallException, ...)

Community
  • 1
  • 1
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
  • 2
    [This](http://php.net/manual/en/language.errors.php7.php) official php documentation was also very helpful for me in understanding this, particularly the Hierarchy defined at the bottom of the page. – kael Jun 27 '16 at 03:04
  • @kael, https://www.php.net/manual/en/class.error.php#126795 (*Lists of Throwable and Exception tree as of 8.1.0*), Script: https://3v4l.org/f8Boe or https://gist.github.com/mlocati/249f07b074a0de339d4d1ca980848e6a – Artfaith Jan 31 '22 at 22:37
2

In addition to @alexander.polomodov and @kael, the below hierarchy may help visualizing supported/built-in exception types which one might use instead of generic Exception or Throwable since these may sometimes result in quite unexpected behavior if caught inappropriately.

Lists of Throwable and Exception tree as of 8.1.0

Error
   ArithmeticError
      DivisionByZeroError
   AssertionError
   CompileError
      ParseError
   FiberError
   TypeError
      ArgumentCountError
   UnhandledMatchError
   ValueError
Exception
   ClosedGeneratorException
   DOMException
   ErrorException
   IntlException
   JsonException
   LogicException
      BadFunctionCallException
         BadMethodCallException
      DomainException
      InvalidArgumentException
      LengthException
      OutOfRangeException
   PharException
   ReflectionException
   RuntimeException
      OutOfBoundsException
      OverflowException
      PDOException
      RangeException
      UnderflowException
      UnexpectedValueException
   SodiumException

Find the script and output in the following links:
https://gist.github.com/mlocati/249f07b074a0de339d4d1ca980848e6a
https://3v4l.org/f8Boe

Source: https://www.php.net/manual/en/class.error.php#126795 [by dams at php dot net, whysteepy at gmail dot com]

Interesting how the syntax highlight library on StackOverflow doesn't colorize 8.0+ changes yet (as of 2021-01; https://i.stack.imgur.com/c5MSn.png).

Artfaith
  • 1,183
  • 4
  • 19
  • 29