欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php?抛异常

程序员文章站 2024-02-13 21:59:34
...
php如何自定义异常类,捕获异常和抛出异常.
或者给个例子参考下


回复讨论(解决方案)

try {      throw new Exception($error);  } catch (Exception $e) {      echo $e->getMessage();  }

try{
throw new Exception($error);
}catch(Exception $e){
echo $e->getCode();
}

set_exception_handler('myException');test(-1);function test($a){	if($a getCode().'
message : '.$e->getMessage(); echo $msg;}

http://www.php.net/manual/zh/language.exceptions.php

function inverse($x) {    if (!$x) {        throw new Exception('Division by zero.');    }    return 1/$x;}try {    echo inverse(5) . "\n";    echo inverse(0) . "\n";} catch (Exception $e) {    echo 'Caught exception: ',  $e->getMessage(), "\n";}// Continue executionecho "Hello World\n";以上例程会输出:0.2Caught exception: Division by zero.Hello World

自定义一个异常类

getMessage());        }    }}$foo = new Test;$foo->testing();?>

try {

throw new Exception($error);

} catch (Exception $e) {

echo $e->getMessage();

}

楼上都是正解

相关标签: php?抛异常