PHP关于异常说明

PHP关于异常说明

1 异常跟PHP错误没关系  所以开启错误和关闭错误都不会影响异常的显示

2 自定义异常

<?php

set_exception_handler('handle_exception');

function handle_exception($exception) {
       echo $exception;
}
throw new Exception("Value must be 1 or below");



checkNum(3);
?>

就是throw new抛出异常的自定义  ps:自定义异常后就不会报错了 如果单单抛出异常会报错

捕获异常

//在 "try" 代码块中触发异常
try
 {
 checkNum(2);
 //If the exception is thrown, this text will not be shown
 echo 'If you see this, the number is 1 or below';
 }

//捕获异常
catch(Exception $e)
 {
 echo 'Message: ' .$e->getMessage();
 }

包括可以捕获自定义异常

以上是PHP7以下的写法


如果遇到什么不懂的地方直接关注公众号留言(本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。)
作者:newmiracle
出处:https://www.cnblogs.com/newmiracle/

 
原文地址:https://www.cnblogs.com/newmiracle/p/15428788.html