Home Backend Development PHP Tutorial Detailed introduction to exception handling in php5 php5 programming

Detailed introduction to exception handling in php5 php5 programming

Jul 29, 2016 am 08:38 AM
php5

1 First, try,catch
<?php
$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
<?php
$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
<?php
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
<?php
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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between php5 and php8 What is the difference between php5 and php8 Sep 25, 2023 pm 01:34 PM

What is the difference between php5 and php8

How to change port 80 in php5 How to change port 80 in php5 Jul 24, 2023 pm 04:57 PM

How to change port 80 in php5

How to solve the problem that php5 is not listening on port 9000 How to solve the problem that php5 is not listening on port 9000 Jul 10, 2023 pm 04:01 PM

How to solve the problem that php5 is not listening on port 9000

What is the difference between php7 and php5 syntax What is the difference between php7 and php5 syntax Jul 10, 2023 pm 03:25 PM

What is the difference between php7 and php5 syntax

What are the differences between the version of php7 and 5? What are the differences between the version of php7 and 5? Sep 15, 2023 pm 04:11 PM

What are the differences between the version of php7 and 5?

How to solve the problem that php5 is not listening on port 9000 How to solve the problem that php5 is not listening on port 9000 Mar 21, 2023 pm 04:32 PM

How to solve the problem that php5 is not listening on port 9000

How to change port 80 in php5 How to change port 80 in php5 Mar 21, 2023 pm 04:32 PM

How to change port 80 in php5

Discuss the syntax differences between PHP7 and PHP5 Discuss the syntax differences between PHP7 and PHP5 Mar 21, 2023 pm 07:10 PM

Discuss the syntax differences between PHP7 and PHP5

See all articles