php对异常的处理与java一样,用到的是try{}catch(){}
定义顶级异常处理器用到的函数是 set_exception_handler("My_exception"); 这里的My_expection是开发者自定义的异常处理函数,既顶级异常处理器,只有当程序中没有函数来处理异常才有顶级异常处理器来处理异常,如果没有定义顶级异常处理器,则由系统默认的异常处理器来处理异常 举例说明:<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>?>
转载自:http://blog.sina.com.cn/s/blog_641d5693010121cr.html