Exceptions
Table of Contents
- BadFunctionCallException
- BadMethodCallException
- DomainException
- InvalidArgumentException
- LengthException
- LogicException
- OutOfBoundsException
- OutOfRangeException
- OverflowException
- RangeException
- RuntimeException
- UnderflowException
- UnexpectedValueException
SPL provides a set of standard Exceptions.
See also the Predefined Exceptions
Коментарии
These exceptions are only provided for documentation purposes. You should not throw them in your code. Instead you should create a base exception that extends the main php Exception class. All exceptions should extend from that base exception. The reason for this is your code will need to be executable under a test environment. The test environment (e.g. PHPUnit) can throw exceptions itself that it needs to be able to catch to tell you when something has failed. If you are throwing and catching the main php exception in your code or SPL exceptions, this will force you to check if the exception type is a phpunit exception when you are catching it. This is of course an unacceptable situation to have test related code in production code.
Don't do this:
<?php
try {
throw new RuntimeException('error message');
catch (RuntimeException $e) {
if ($e instanceof PHPUnit_Framework_Exception) {
throw $e;
}
echo $e->getMessage();
}
?>
Do this:
<?php
class MyRuntimeException extends Exception {
}
try {
throw new MyRuntimeException('error message');
catch (MyRuntimeException $e) {
echo $e->getMessage();
}
?>