SoFunction
Updated on 2025-03-10

Introduction to the detailed method of exception handling in php5 programming

1. First of all, try, catch

<?php 
$path = "D:\\\\"; 
try //Detection of 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 cannot be opened", 2);


?> 
Pay attention to using $e->getMessage() to output exception information.

2 Output exception complete information

<?php 
$path = "D:\\\\"; 

try 

file_open($path);//Try to open the file

catch(Exception $e) 

echo "Exception information: ".$e->getMessage()."\\n"; //Return user-defined exception information
echo "Exception code: ".$e->getCode()."\\n"; //Return the 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 number of the line where the exception code is located.
echo “Delivery route:”;
print_r($e->getTrace()); //Return the route passed in each step of the tracking exception in the form of an array.
echo $e->getTraceAsString(); //Return the getTrace function information formatted into 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 exception, that is, custom exception

<?php 
class FileExistsException extends Exception{} //Class used to deal with files that do not have exceptions
class FileOpenException extends Exception{} //Class used to handle file unreadable exceptions

$path = "D:\\\\"; 

try 

file_open($path); 

catch(FileExistsException $e)//If a FileExistsException exception is generated, the user will be prompted to confirm the file location.

echo "Exception occurred during the program: ".$e->getMessage()."\\n";
echo "Please confirm the file location.";

catch(FileOpenException $e)//If a FileOpenException exception is generated, the user will be prompted to confirm the readability of the file.

echo "Exception occurred during 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 user-defined exception information
echo "Exception code: ".$e->getCode()."\\n"; //Return the 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 number of the line where the exception code is located.
echo “Delivery route:”;
print_r($e->getTrace()); //Return the route passed in each step of the tracking exception in the form of an array.
echo $e->getTraceAsString(); //Return the getTrace function information formatted into a string


function file_open($path) 

if(!file_exists($path)) 

throw new FileExistsException("File cannot be found", 1); //Top the FileExistsException exception object


if(!fopen($path, "r")) 

throw new FileOpenException("File cannot be opened", 2); //Top the FileOpenException exception object



?> 

4 Resell the exception to the upper level

<?php 
class FileExistsException extends Exception{} //Class used to deal with files that do not have exceptions
class FileOpenException extends Exception{} //Class used to handle file unreadable exceptions

$path = "D:\\\\"; 

try 

file_open($path); 

catch(FileExistsException $e)//If a FileExistsException exception is generated, the user will be prompted to confirm the file location.

echo "Exception occurred during the program: ".$e->getMessage()."\\n";
echo "Please confirm the file location.";

catch(FileOpenException $e)//If a FileOpenException exception is generated, the user will be prompted to confirm the readability of the file.

echo "Exception occurred during 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 user-defined exception information
echo "Exception code: ".$e->getCode()."\\n"; //Return the 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 number of the line where the exception code is located.
echo “Delivery route:”;
print_r($e->getTrace()); //Return the route passed in each step of the tracking exception in the form of an array.
echo $e->getTraceAsString(); //Return the getTrace function information formatted 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 "Exception occurred during operation of file_open function";
throw $e; //Rethrow exception


?>