Home> php教程> php手册> body text

php异常处理技术,顶级异常处理器

WBOY
Release: 2016-06-13 11:59:49
Original
1031 people have browsed it

定义顶级异常处理器用到的函数是
set_exception_handler("My_exception");
这里的My_expection是开发者自定义的异常处理函数,既顶级异常处理器,只有当程序中没有函数来处理异常才有顶级异常处理器来处理异常,如果没有定义顶级异常处理器,则由系统默认的异常处理器来处理异常

举例说明:

复制代码代码如下:


set_exception_handler("My_expection");
function My_expection(){
echo "这里是顶级异常处理器";
}
try{
nohello("hello");
}catch(Exception $e){
throw $e;
}
  function nohello($nohello){
   if($nohello == "hello"){
   throw new Exception("不能输入hello");
   }else{
echo "输入成功";
}
}
?>


关于PHP异常处理 的一个问题

复制代码代码如下:


try {
$a=10/0;
}catch (Exception $e){
echo "抛出异常";
}
?>


以上代码,并不会输出“抛出异常”,而是输出了错误信息:Warning: Division by zero in......

说明:try块中并没有抛出异常,而是按照系统默认的错误处理机制进行处理

所以能不能捕获到异常,取决于有没有真正的抛出异常
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!