您的位置 首页 php

try catch在PHP中的使用

1.try catch可以捕获上一层throw的异常

2.finally是不管try或者catch任何一块有return, 最终都会执行的块

3.try也是可以捕获到call_user_func_array回调函数类内部的throw的异常

4.call_user_func_array只能回调类的静态方法,可以在这个静态方法中进行new对象

5.在不自定义任何错误处理函数的情况下,try是不能捕获php本身的错误的,包括notice warning error等级别

下面的代码是项目中的一个部分,经过了多层调用和回调

<?phpclass Oss {    public static function connect() {    throw new Exception("oss connect error");    return 'oss object';    }}//调用三层class S3{public static function connect() {//throw new Exception("s3 connect error");    return 's3 object';    }}//调用二层function callReader($class,$url){try{$conn=call_user_func_array(array($class, "connect"),array());return $conn;}catch(Exception $e){throw $e;}finally{//无论如何都会执行,在这记录日志}}//调用一层function getMessage(){$conn=null;try {    $conn=callReader('Oss',"http://xxxx");} catch (Exception $e1) {$conn=callReader('S3',"http://xxxx");}return $conn;}//最先的入口try{var_dump(getMessage());}catch(Exception $e){}

【课程推荐:PHP视频教程】  

以上就是try catch在PHP中的使用的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:try catch在PHP中的使用

文章地址:https://www.zhihuclub.com/5866.shtml

关于作者: 智云科技

热门文章

网站地图