Exceptions in PHP7
Many PHP developers still using Exceptions in the old PHP5 style. It is ok. There is nothing wrong. But moving forward you can, and probably should use new features from PHP7.
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.
Hierarchy of Error types
Here's a chart that demonstrates the new hierarchy introduced in PHP 7:
\Throwable ├── \Exception implements \Throwable │ ├── \LogicException extends \Exception │ │ ├── \BadFunctionCallException extends \LogicException │ │ │ └── \BadMethodCallException extends \BadFunctionCallException │ │ ├── \DomainException extends \LogicException │ │ ├── \InvalidArgumentException extends \LogicException │ │ ├── \LengthException extends \LogicException │ │ └── \OutOfRangeException extends \LogicException │ └── \RuntimeException extends \Exception │ ├── \OutOfBoundsException extends \RuntimeException │ ├── \OverflowException extends \RuntimeException │ ├── \RangeException extends \RuntimeException │ ├── \UnderflowException extends \RuntimeException │ └── \UnexpectedValueException extends \RuntimeException └── \Error implements \Throwable ├── \AssertionError extends \Error ├── \ParseError extends \Error ├── \TypeError extends \Error │ └── \ArgumentCountError extends \TypeError └── \ArithmeticError extends \Error └── \DivisionByZeroError extends \ArithmeticError
So here is the description of some of them:
Throwableis the base interface for any object that can be thrown via a throw statement in PHP 7, including Error and Exception.
Erroris the base class for all internal PHP errors.
AssertionErroris thrown when an assertion made via assert() fails.
ParseErroris thrown when an error occurs while parsing PHP code, such as when eval() is called.
TypeErrorThere are three scenarios where a TypeError may be thrown. The first is where the argument type being passed to a function does not match its corresponding declared parameter type. The second is where a value being returned from a function does not match the declared function return type. The third is where an invalid number of arguments are passed to a built-in PHP function (strict mode only).
ArithmeticErroris thrown when an error occurs while performing mathematical operations. In PHP 7.0, these errors include attempting to perform a bit shift by a negative amount, and any call to intdiv() that would result in a value outside the possible bounds of an integer.
DivisionByZeroErroris thrown when an attempt is made to divide a number by zero.
ArgumentCountErroris thrown when too few arguments are passed to a user-defined function or method.
Catching Multiple Exception Types
According to RFC you can use several
catch blocks. Be sure that you have PHP 7.1
This one you might know. You can rethrow the exception
You can also use block
A little demo for using Exceptions, Errors and handle them:
Remember! Errors will continue executing PHP application while Exceptions stop executing it!