getMessage(); } function file_open($path) { if(!file_exists($path"/> getMessage(); } function file_open($path) { if(!file_exists($path">

Home  >  Article  >  Backend Development  >  Detailed introduction to exception handling in php5 php5 programming

Detailed introduction to exception handling in php5 php5 programming

WBOY
WBOYOriginal
2016-07-29 08:38:30777browse

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