LogicException:PHP5.1起,表示程序逻辑中的错误,这种异常应当由代码修复。
BadFunctionCallException:PHP5.1起,LogicException的子类,如果回调函数引用的是未定义函数或参数缺失则抛出异常,一个典型的用法是与is_callable函数结合使用。
DomainException:PHP5.1起,LogicException的子类,如果值不在有效的数据范围内则抛出异常。
InvalidArgumentException:PHP5.1起,LogicException的子类,如果参数不是预期的类型则抛出异常。
LengthException:PHP5.1起,LogicException的子类,如果长度无效则抛出异常。
OutOfRangeException:PHP5.1起,LogicException的子类,当请求非法索引时抛出异常,检测编译时可知的错误。
BadMethodCallException:PHP5.1起,BadFunctionCallException的子类,如果回调函数引用的是未定义方法或参数缺失则抛出异常。
RuntimeException:PHP5.1起,运行时发生错误会抛出异常。
OutOfBoundsException:PHP5.1起,RuntimeException的子类,如果不是有效的键则抛出异常,表示编译时无法检测的错误。
OverflowException:PHP5.1起,RuntimeException的子类,添加元素到已满的容器中则抛出异常。
RangeException:PHP5.1起,RuntimeException的子类,在程序运行时出现范围错误时抛出异常,通常除了范围错误同时还有算术错误,是DomainException的运行时版本。
UnderflowException:PHP5.1起,RuntimeException的子类,在空容器上执行无效操作时抛出异常。
BadFunctionCallException:PHP5.1起,LogicException的子类,如果回调函数引用的是未定义函数或参数缺失则抛出异常,一个典型的用法是与is_callable函数结合使用。
DomainException:PHP5.1起,LogicException的子类,如果值不在有效的数据范围内则抛出异常。
InvalidArgumentException:PHP5.1起,LogicException的子类,如果参数不是预期的类型则抛出异常。
LengthException:PHP5.1起,LogicException的子类,如果长度无效则抛出异常。
OutOfRangeException:PHP5.1起,LogicException的子类,当请求非法索引时抛出异常,检测编译时可知的错误。
BadMethodCallException:PHP5.1起,BadFunctionCallException的子类,如果回调函数引用的是未定义方法或参数缺失则抛出异常。
RuntimeException:PHP5.1起,运行时发生错误会抛出异常。
OutOfBoundsException:PHP5.1起,RuntimeException的子类,如果不是有效的键则抛出异常,表示编译时无法检测的错误。
OverflowException:PHP5.1起,RuntimeException的子类,添加元素到已满的容器中则抛出异常。
RangeException:PHP5.1起,RuntimeException的子类,在程序运行时出现范围错误时抛出异常,通常除了范围错误同时还有算术错误,是DomainException的运行时版本。
UnderflowException:PHP5.1起,RuntimeException的子类,在空容器上执行无效操作时抛出异常。
UnexpectedValueException:PHP5.1起,RuntimeException的子类,如果一个值与一组值不匹配则抛出异常。
<?php
$arr1=[
'a'=>'a',
'b'=>'b',
'c'=>'c',
];
$arr2=[
'a'=>'a',
'b'=>'b',
'c'=>'c',
'd'=>'d',
];
function foo($arg){
if(is_string($arg)){
if(strlen($arg)>=2){
$func = 'test' . $arg;
if(is_callable($func)){
return $func;
}else{
throw new BadFunctionCallException('function '. $func.' is not callable.');
}
}else{
throw new LengthException('the minimum length of argument $arg is 2.');
}
}else{
throw new InvalidArgumentException('function foo only accepts string.');
}
}
function testDomainException($type){
switch($type){
case 1:
case 2:echo "less than 3\n"; break;
case 3:
case 4:echo "less than 5\n"; break;
default:
throw new DomainException('Unknown type: '.$type."\n");
}
echo "function doTest\n";
}
function testOutOfRangeException($index){
$arr=['one'=>1,
'two'=>2,
'three'=>3,
];
if(!isset($arr[$index])){
throw new OutOfRangeException('item '.$index." is not exists");
}
}
function testOutOfBoundsException($chars){
if(!isset($chars['d'])){
throw new OutOfBoundsException('item d is not exists in $chars.');
}
}
function testOverflowException(&$letters,$abc){
if(count($letters)>=5){
throw new OverflowException('$letters is full.');
}else{
$letters[$abc]=$abc;
}
}
function testRangeException($number){
if($number==0){
throw new RangeException("Divisor must not be zero");
}else{
echo "the reciprocal of $number is ".(1/$number)."\n";
}
}
function testUnderflowException($arr){
if(count($arr)==0){
throw new UnderflowException("\$arr is empty");
}else{
array_pop($arr);
}
}
function testUnexpectedValueException($type){
if($type!='foo'&&$type!='bar'){
throw new UnexpectedValueException("unexpected value $type");
}
}
try{
//foo("DomainException")(5);//DomainException
//foo("a")();//LengthException
//foo("ab")();//BadFunctionCallException
//foo(123)();//InvalidArgumentException
//foo("OutOfRangeException")('four');//OutOfRangeException
//foo("OutOfBoundsException")($arr1);//OutOfBoundsException
//foo("OverflowException")($arr2,'e');
//foo("OverflowException")($arr2,'f');//OverflowException
//foo("RangeException")(0);//RangeException
//foo("UnderflowException")([]);//UnderflowException
//foo("UnexpectedValueException")("boo");//UnexpectedValueException
}catch(BadFunctionCallException $e){
echo $e->getMessage()."\n";
}catch(InvalidArgumentException $e){
echo $e->getMessage()."\n";
}catch(DomainException $e){
echo $e->getMessage()."\n";
}catch(LengthException $e){
echo $e->getMessage()."\n";
}catch(OutOfRangeException $e){
echo $e->getMessage()."\n";
}catch(OutOfBoundsException $e){
echo $e->getMessage()."\n";
}catch(OverflowException $e){
echo $e->getMessage()."\n";
}catch(RangeException $e){
echo $e->getMessage()."\n";
}catch(UnderflowException $e){
echo $e->getMessage()."\n";
}catch(UnexpectedValueException $e){
echo $e->getMessage()."\n";
}
?>