getMessage(); } function file_open($path) { if(!file_exists($path"/> getMessage(); } function file_open($path) { if(!file_exists($path">
search
HomeBackend DevelopmentPHP TutorialDetailed introduction to exception handling in php5 php5 programming

1 First, try,catch
$path = "D:\\in.txt";
try //Detect exception
{
file_open($path);
}
catch(Exception $e) / /Catch exception
{
echo $e->getMessage();
}
function file_open($path)
{
if(!file_exists($path)) //If the file cannot be found, throw an exception object
{
throw new Exception("File cannot be found", 1);
}
if(!fopen($path, "r")) //If the file cannot be opened, throw an exception object
{
throw new Exception("File Unable to open", 2);
}
}
?>
Please use $e->getMessage() to output exception information.
2 Output complete exception information
$path = "D:\\ in.txt";
try
{
file_open($path); //Try to open the file
}
catch(Exception $e)
{
echo "Exception message: ".$e->getMessage()." \n"; //Return user-defined exception information
echo "Exception code: ".$e->getCode()."\n"; //Return user-defined exception code
echo "File name: ".$e->getFile()."\n"; //Return the name of the PHP program file where the exception occurred
echo "The line where the exception code is located".$e->getLine()."\n"; / /Return the line number of the line where the exception occurs
echo "Transmission route:";
print_r($e->getTrace()); //Return the route of each step of the tracking exception in the form of an array
echo $e- >getTraceAsString(); //Return getTrace function information formatted as a string
}
function file_open($path)
{
if(!file_exists($path)) //If the file does not exist, an error is thrown
{
throw new Exception("File cannot be found", 1);
}
if(!fopen($path, "r"))
{
throw new Exception("File cannot be opened", 2);
}
}
?>
Extended exceptions, that is, custom exceptions
class FileExistsException extends Exception{} //A class for processing files that does not exist
class FileOpenException extends Exception{} //Cannot be used for processing files Read exception class
$path = "D:\\in.txt";
try
{
file_open($path);
}
catch(FileExistsException $e) //If a FileExistsException exception occurs, the user is prompted to confirm the file location
{
echo "An exception occurred during the running of the program: ".$e->getMessage()."\n";
echo "Please confirm the file location. ";
}
catch(FileOpenException $e) //If a FileOpenException occurs, the user is prompted to confirm the readability of the file
{
echo "An exception occurred during the running of the program: ".$e->getMessage() ."\n";
echo "Please confirm the readability of the file.";
}
catch(Exception $e)
{
echo "[Unknown exception]";
echo "Exception information: ".$e->getMessage()."\n"; //Return to user-defined Exception information
echo "Exception code: ".$e->getCode()."\n"; //Return user-defined exception code
echo "File name: ".$e->getFile() ."\n"; //Return the PHP program file name where the exception occurred
echo "The line where the exception code is located".$e->getLine()."\n"; //Return the line where the exception code is located No.
echo "Transmission route:";
print_r($e->getTrace()); //Return the route of each step of the tracking exception in the form of an array
echo $e->getTraceAsString(); //Return format GetTrace function information converted into a string
}
function file_open($path)
{
if(!file_exists($path))
{
throw new FileExistsException("File cannot be found", 1); //Throw FileExistsException Object
}
if(!fopen($path, "r"))
{
throw new FileOpenException("File cannot be opened", 2); //Throw FileOpenException exception object
}
}
?>
4 Rethrow the exception to the upper layer
class FileExistsException extends Exception{} //A class used to handle file non-existence exceptions
class FileOpenException extends Exception{} //A class used to handle file unreadable exceptions
$path = "D:\\in.txt";
try
{
file_open($path);
}
catch(FileExistsException $e) //If a FileExistsException exception occurs, the user is prompted to confirm the file location
{
echo "The program is running An exception occurred during the process: ".$e->getMessage()."\n";
echo "Please confirm the file location. ";
}
catch(FileOpenException $e) //If a FileOpenException occurs, the user is prompted to confirm the readability of the file
{
echo "An exception occurred during the running of the program: ".$e->getMessage() ."\n";
echo "Please confirm the readability of the file. ";
}
catch(Exception $e)
{
echo "[Unknown exception]";
echo "Exception information: ".$e->getMessage()."\n"; //Return to user-defined Exception information
echo "Exception code: ".$e->getCode()."\n"; //Return user-defined exception code
echo "File name: ".$e->getFile() ."\n"; //Return the name of the PHP program file where the exception occurred
echo "The line where the exception code is located".$e->getLine()."\n"; //Return the line where the exception code is located No.
echo "Transmission route:";
print_r($e->getTrace()); //Return the route of each step of the tracking exception in the form of an array
echo $e->getTraceAsString(); //Return format GetTrace function information converted into a string
}
function file_open($path)
{
try
{
if(!file_exists($path))
{
throw new FileExistsException("File cannot be found", 1);
}
if(!fopen($path, "r"))
{
throw new FileOpenException("File cannot be opened", 2);
}
}
catch(Exception $e) //Catch exception
{
echo " An exception occurred during the operation of the file_open function";
throw $e; //Rethrow exception
}
}
?>

The above has introduced the detailed method of exception handling in php5 php5 programming, including php5 content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php5和php8有什么区别php5和php8有什么区别Sep 25, 2023 pm 01:34 PM

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

php5如何改80端口php5如何改80端口Jul 24, 2023 pm 04:57 PM

php5改80端口的方法:1、编辑Apache服务器的配置文件中的端口号;2、辑PHP的配置文件以确保PHP在新端口上工作;3、重启Apache服务器,PHP应用程序将开始在新的端口上运行。

php5没有监听9000端口如何解决php5没有监听9000端口如何解决Jul 10, 2023 pm 04:01 PM

php5没有监听9000端口解决方法步骤:1、检查PHP-FPM配置文件;2、重启PHP-FPM服务;3、关闭防火墙或配置端口转发;4、检查其他进程是否占用9000端口。

php7和php5语法有什么区别php7和php5语法有什么区别Jul 10, 2023 pm 03:25 PM

php7和php5语法区别有:1、PHP7引入了严格的类型声明,而PHP5变量的类型是隐式的;2、PHP7引入了对标量类型声明的支持,而PHP5并没有;3、PHP7引入了NULL合并运算符,而PHP5检查一个变量是否存在并且不为null,需要使用条件语句;4、PHP7添加了新的比较运算符“<=>”,而PHP5并没有;5、PHP7引入新特性匿名类,而PHP5并没有。

php7的版本和5有哪些不同php7的版本和5有哪些不同Sep 15, 2023 pm 04:11 PM

php7的版本和5的不同有性能提升、标量类型声明、返回值类型声明、异常处理改进、匿名类、语法改进、新的运算符、增强的错误处理和移除了一些旧特性等。详细介绍:1、性能提升,PHP7引入了全新的Zend引擎,名为Zend Engine 3.0,带来了显著的性能提升,PHP7的性能比PHP5提高了大约两倍,这主要是通过改进了内存管理、优化了函数调用和异常处理、增强了垃圾回收等等。

php5没有监听9000端口怎么解决php5没有监听9000端口怎么解决Mar 21, 2023 pm 04:32 PM

在使用PHP5时,有些用户可能会遇到无法监听端口9000的情况。这时,我们需要进行一些配置和排查工作来解决这一问题。

php5怎么改80端口php5怎么改80端口Mar 21, 2023 pm 04:32 PM

​如果你是一名网站管理员,你可能会碰到需要将 PHP5 的端口从默认的 80 端口进行修改的情况。这个过程可能会稍有进急,但是只要跟着以下步骤操作,一定会很容易地完成。

探讨PHP7和PHP5在语法上的不同之处探讨PHP7和PHP5在语法上的不同之处Mar 21, 2023 pm 07:10 PM

PHP是一种广泛使用的服务器端编程语言,被用于开发动态网站和应用程序。近些年,PHP7的发布引起了一些关注。PHP7比之前的版本(如PHP5)有很多改进和优化。在本文中,我们将探讨PHP7和PHP5在语法上的不同之处。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

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.