php無法捕獲異常怎麼辦

coldplay.xixi
發布: 2023-03-05 15:44:01
原創
3823 人瀏覽過

解决php无法捕获异常的方法:首先使用函数【try,catch】输出异常完整信息;然后定义异常,代码为【catch(FileExistException $e)】;最后重抛异常给上层,代码为【catch(Exception $e)】。

php無法捕獲異常怎麼辦

【相关学习推荐:php图文教程

解决php无法捕获异常的方法:

1、首先是try,catch

  

getMessage();
}
function file_open($path)
{
 if(!file_exists($path))       //如果文件无法找到,抛出异常对象
 {
  throw new Exception("文件无法找到", 1);
 }
 
 if(!fopen($path, "r"))       //如果文件无法打开,抛出异常对象
 {
  throw new Exception("文件无法打开", 2);
 }
}
?>
登入後複製

注意用$e->getMessage()输出异常信息.

2、输出异常完整信息

getMessage()."\n";   //返回用户自定义的异常信息
 echo "异常代码:".$e->getCode()."\n";      //返回用户自定义的异常代码
 echo "文件名:".$e->getFile()."\n";                    //返回发生异常的PHP程序文件名
 echo "异常代码所在行".$e->getLine()."\n";               //返回发生异常的代码所在行的行号
 echo "传递路线:";
 print_r($e->getTrace());                     //以数组形式返回跟踪异常每一步传递的路线
 echo $e->getTraceAsString();              //返回格式化成字符串的getTrace函数信息
}
function file_open($path)
{
 if(!file_exists($path))        //如果文件不存在,则抛出错误
 {
  throw new Exception("文件无法找到", 1);
 }
 
 if(!fopen($path, "r"))
 {
  throw new Exception("文件无法打开", 2);
 }
}
?>
登入後複製

3、扩展异常,即自定义异常

getMessage()."\n";
 echo "请确认文件位置。";
}
catch(FileOpenException $e)    //如果产生FileOpenException异常则提示用户确认文件的可读性
{
 echo "程序在运行过程中发生了异常:".$e->getMessage()."\n";
 echo "请确认文件的可读性。";
}
catch(Exception $e)
{
 echo "[未知异常]";
 echo "异常信息:".$e->getMessage()."\n";   //返回用户自定义的异常信息
 echo "异常代码:".$e->getCode()."\n";      //返回用户自定义的异常代码
 echo "文件名:".$e->getFile()."\n";                    //返回发生异常的PHP程序文件名
 echo "异常代码所在行".$e->getLine()."\n";    //返回发生异常的代码所在行的行号
 echo "传递路线:";
 print_r($e->getTrace());                //以数组形式返回跟踪异常每一步传递的路线
 echo $e->getTraceAsString();              //返回格式化成字符串的getTrace函数信息
}
function file_open($path)
{
 if(!file_exists($path))
 {
  throw new FileExistsException("文件无法找到", 1);  //抛出FileExistsException异常对象
 }
 
 if(!fopen($path, "r"))
 {
  throw new FileOpenException("文件无法打开", 2);   //抛出FileOpenException异常对象
 }
}
?>
登入後複製

4、重抛异常给上层

getMessage()."\n";
 echo "请确认文件位置。";
}
catch(FileOpenException $e)    //如果产生FileOpenException异常则提示用户确认文件的可读性
{
 echo "程序在运行过程中发生了异常:".$e->getMessage()."\n";
 echo "请确认文件的可读性。";
}
catch(Exception $e)
{
 echo "[未知异常]";
 echo "异常信息:".$e->getMessage()."\n";   //返回用户自定义的异常信息
 echo "异常代码:".$e->getCode()."\n";      //返回用户自定义的异常代码
 echo "文件名:".$e->getFile()."\n";                    //返回发生异常的PHP程序文件名
 echo "异常代码所在行".$e->getLine()."\n";               //返回发生异常的代码所在行的行号
 echo "传递路线:";
 print_r($e->getTrace());                   //以数组形式返回跟踪异常每一步传递的路线
 echo $e->getTraceAsString();              //返回格式化成字符串的getTrace函数信息
}
function file_open($path)
{
 try
 {
  if(!file_exists($path))
  {
   throw new FileExistsException("文件无法找到", 1);
  }
  
  if(!fopen($path, "r"))
  {
   throw new FileOpenException("文件无法打开", 2);
  }
 }
 catch(Exception $e)        //捕获异常
 {
  echo "file_open函数在运行过程中出现异常";
  throw $e;         //重掷异常
 }
}
?>
登入後複製

相关学习推荐:php编程(视频)

以上是php無法捕獲異常怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!