php handles exceptions the same as java, using try{}catch() {}
The function used to define the top-level exception handler is set_exception_handler("My_exception"); My_expection here is a developer-defined exception handling function, which is a top-level exception handler. Only when there is no function in the program to handle exceptions will there be a top-level exception handler to handle exceptions. If no top-level exception handler is defined, the system will default to it. exception handler to handle exceptions Example:<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?<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>?>
Reprinted from: http://blog.sina.com.cn/s/blog_641d5693010121cr.html