我们在开发中,没有使用exception异常的习惯,在项目中不会使用exception,
今天看到一位大佬写的代码,使用了很多exception,
什么是异常?
异常就是可预测但是又没办法消除的一种错误
如果发生异常而不去处理,会导致程序中断,也就是程序无法继续运行。
为什么要使用exception?
给用户一个良好的出错提示
其实我们经常在系统中,阻止系统正常运行,比如说
exit
die
等函数。
现在逐步使用 throw new RuntimeException() 这样的方式替代 exit() 这样的命令。
使用异常要用
try{
throw new RuntimeException('系统异常');
}catch(Exception $e){
}
去捕获异常。
php中提供了哪几种异常?
class LogicException extends Exception {
}
class BadFunctionCallException extends LogicException {
}
class BadMethodCallException extends BadFunctionCallException {
}
class DomainException extends LogicException {
}
class InvalidArgumentException extends LogicException {
}
class LengthException extends LogicException {
}
class OutOfRangeException extends LogicException {
}
class RuntimeException extends Exception {
}
class OutOfBoundsException extends RuntimeException {
}
class OverflowException extends RuntimeException {
}
class RangeException extends RuntimeException {
}
class UnderflowException extends RuntimeException {
}
class UnexpectedValueException extends RuntimeException {
}
InvalidArgument
LengthException
LogicException
OutOfBoundsException
OutOfRangeException
OverflowException
RangeException
RuntimeException
UnderflowException
UnexpectedValueException
LogicException
InvalidArgument
LengthException
LogicException
OutOfBoundsException
OutOfRangeException
OverflowException
RangeException
RuntimeException
UnderflowException
UnexpectedValueException
LogicException
无效参数
长例外
逻辑异常
异常输出异常
异常除外
溢出异常
范围异常
运行期异常
下溢异常
意外值异常
逻辑异常