1 首先是try,catch
$path = "D:\\\\in.txt";
try //检测异常
{
file_open($path);
}
catch(Exception $e) //捕获异常
{
echo $e->getMessage();
}
function file_open($path)
{
if(!file_exists($path)) //如果文件无法找到,抛出异常对象
{
throw new Exception("文件无法找到", 1);
}
if(!fopen($path, "r")) //如果文件无法打开,抛出异常对象
{
throw new Exception("文件无法打开", 2);
}
}
?>
注意用$e->getMessage()输出异常信息.
2 输出异常完整信息
$path = "D:\\\\in.txt";
try
{
file_open($path); //尝试打开文件
}
catch(Exception $e)
{
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 Exception("文件无法找到", 1);
}
if(!fopen($path, "r"))
{
throw new Exception("文件无法打开", 2);
}
}
?>
扩展异常,即自定义异常
class FileExistsException extends Exception{} //用于处理文件不存在异常的类
class FileOpenException extends Exception{} //用于处理文件不可读异常的类
$path = "D:\\\\in.txt";
try
{
file_open($path);
}
catch(FileExistsException $e) //如果产生FileExistsException异常则提示用户确认文件位置
{
echo "程序在运行过程中发生了异常:".$e->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 重抛异常给上层
class FileExistsException extends Exception{} //用于处理文件不存在异常的类
class FileOpenException extends Exception{} //用于处理文件不可读异常的类
$path = "D:\\\\in.txt";
try
{
file_open($path);
}
catch(FileExistsException $e) //如果产生FileExistsException异常则提示用户确认文件位置
{
echo "程序在运行过程中发生了异常:".$e->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; //重掷异常
}
}
?>

在Java中,当多个线程同时操作一个集合对象时,有可能会发生ConcurrentModificationException异常,该异常通常发生在遍历集合时进行修改或者删除元素的操作,这会导致集合的状态出现不一致,从而抛出异常。本文将深入探讨该异常的产生原因和解决方法。一、异常产生原因通常情况下,ConcurrentModificationException异

在当今科技快速发展的时代,编程语言也如雨后春笋般涌现出来。其中一门备受瞩目的语言就是Go语言,它以其简洁、高效、并发安全等特性受到了许多开发者的喜爱。Go语言以其强大的生态系统而著称,其中有许多优秀的开源项目。本文将介绍五个精选的Go语言开源项目,带领读者一起探索Go语言开源项目的世界。KubernetesKubernetes是一个开源的容器编排引擎,用于自

《Go语言开发必备:5个热门框架推荐》Go语言作为一门快速、高效的编程语言,受到越来越多开发者的青睐。为了提高开发效率,优化代码结构,很多开发者选择使用框架来快速搭建应用。在Go语言的世界中,有许多优秀的框架可供选择。本文将介绍5个热门的Go语言框架,并提供具体的代码示例,帮助读者更好地理解和使用这些框架。1.GinGin是一个轻量级的Web框架,拥有快速

随着互联网的发展和信息技术的进步,大数据时代已经来临,数据分析、机器学习等领域也得到了广泛的应用。在这些领域中,任务调度是一个不可避免的问题。如何实现高效的任务调度,对于提高效率至关重要。在本篇文章中,将介绍如何使用Golang的Web框架Echo框架实现分布式任务调度。一、介绍Echo框架Echo是一个高性能、可伸缩、轻量级的GoWeb框架。它基于HTT

Laravel是一个流行的PHP框架,具有高度可扩展性和高效性,它提供了很多强大的工具和库,让开发者可以快速构建高质量的Web应用程序。其中,LaravelEcho和Pusher是两个非常重要的工具,通过它们可以很容易地实现WebSockets通信,本文将详细介绍如何在Laravel应用程序中使用这两个工具。什么是WebSockets?WebSockets

PHP中echo关键字的作用和使用方法详解PHP是一种广泛使用的服务器端脚本语言,它在网页开发中被广泛应用。而echo关键字是在PHP中用于输出内容的一种方法。本文将详细介绍echo关键字的作用和使用方法。作用:echo关键字的主要作用是将内容输出到浏览器。在网页开发中,我们需要将数据动态地呈现到前端页面上,这时就可以使用echo关键字将数据输出到页面上。e

Go语言作为一种快速、高效的编程语言,一直受到程序员的青睐。在Go语言的生态系统中,框架扮演着至关重要的角色,帮助开发者更快速地构建应用程序。本文将介绍五个Go语言框架,让你了解其特点和用法。1.Gin框架Gin框架是一个轻量级的Web框架,具有快速、高性能的特点。使用Gin框架可以快速构建RESTfulAPI和Web应用程序。以下是一个简单的示例代码:

Java中的UnsupportedEncodingException异常该如何处理?在Java编程中,可能会遇到UnsupportedEncodingException异常。这个异常通常是由于编码转换不正确或编码不支持造成的。在这篇文章中,我们将介绍UnsupportedEncodingException异常的原因和如何处理它。什么是UnsupportedE


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
