异常是程序执行过程中出现的问题。在程序执行过程中,发生异常时,该语句后面的代码将不会被执行,PHP 将尝试查找第一个匹配的 catch 块。如果未捕获异常,则会发出 PHP 致命错误,并显示“未捕获异常”。
语法
try { print "this is our try block"; throw new Exception(); }catch (Exception $e) { print "something went wrong, caught yah! n"; }finally { print "this part is always executed"; }
登录后复制
Example
的中文翻译为:
示例
After throw (It will never be executed)"; } } // When Exception has been thrown by try block catch(Exception $e){ echo "Exception Caught", $e->getMessage(); } //this block code will always executed. finally{ echo "
Final block will be always executed"; } } // Exception will not be rised here printdata(0); // Exception will be rised printdata(6); ?>
登录后复制
输出
Final block will be always executed Exception CaughtNumber is six. Final block will be always executed
登录后复制
注意
要处理异常,程序代码必须位于 try 块内。每次尝试必须至少有一个相应的 catch 块。多个catch块可用于捕获不同类别的异常。
以上就是PHP中的异常处理是什么?的详细内容,更多请关注php中文网其它相关文章!