Home > php教程 > php手册 > php异常处理技术,顶级异常处理器,php异常处理

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

WBOY
Release: 2016-06-13 08:41:48
Original
1028 people have browsed it

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

php对异常的处理与java一样,用到的是try{}catch(){}

定义顶级异常处理器用到的函数是 set_exception_handler("My_exception"); 这里的My_expection是开发者自定义的异常处理函数,既顶级异常处理器,只有当程序中没有函数来处理异常才有顶级异常处理器来处理异常,如果没有定义顶级异常处理器,则由系统默认的异常处理器来处理异常 举例说明:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
Copy after login
<?<span>php
    </span><span>set_exception_handler</span>("My_expection"<span>);
    </span><span>function</span><span> My_expection(){
        </span><span>echo</span> "这里是顶级异常处理器"<span>;
    }
    </span><span>try</span><span>{
        nohello(</span>"hello"<span>);
    }</span><span>catch</span>(<span>Exception</span> <span>$e</span><span>){
        </span><span>throw</span> <span>$e</span><span>;
    }
  </span><span>function</span> nohello(<span>$nohello</span><span>){
          </span><span>if</span>(<span>$nohello</span> == "hello"<span>){
              </span><span>throw</span> <span>new</span> <span>Exception</span>("不能输入hello"<span>);
          }</span><span>else</span><span>{
            </span><span>echo</span> "输入成功"<span>;
        }
    }
</span>?>
Copy after login

转载自:http://blog.sina.com.cn/s/blog_641d5693010121cr.html

Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template