教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 try catch在PHP中的使用

try catch在PHP中的使用

发布时间:2021-01-07   编辑:jiaochengji.com
教程集为您提供try catch在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等级别

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


<?php
class 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中的使用的详细内容,更多请关注教程集其它相关文章!

  • 本文转载于:博客园,如有侵犯,请联系jquerycn@qq.com删除
  • 您可能感兴趣的文章:
    try catch在PHP中的使用
    php异常捕获try catch实例解析
    php try catch必要吗
    java 7中捕获多个异常示例分析
    PHP异常处理(Exception)什么情况下使用?
    再次学习try catch finally
    Golang模仿try & catch
    php中try catch 捕获异常的例子
    使用php异常处理类Exception的例子
    Javascript try…catch… 语句的用法

    [关闭]
    ~ ~