


Detailed introduction to exception handling in php5 php5 programming
Jul 29, 2016 am 08:38 AM1 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.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What is the difference between php5 and php8

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

What is the difference between php7 and php5 syntax

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

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

Discuss the syntax differences between PHP7 and PHP5
