1 First, try,catch
$path = "D:\\in.txt";
try //Detect exceptions
{
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("The file cannot be opened" , 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 message
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 getTrace function information formatted into a string
}
function file_open($path)
{
if(!file_exists($path)) //If the file does not If exists, 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 exceptions
class FileExistsException extends Exception{} //A class used to process files without exceptions
class FileOpenException extends Exception{} //Used to process files Class of unreadable exception
$path = "D:\\in.txt";
try
{
file_open($path);
}
catch(FileExistsException $e) //If a FileExistsException 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 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 occurred
echo "Transmission route:";
print_r($e->getTrace()); //Return the tracking exception in the form of an array The route passed at each step
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); //Throw FileExistsException exception 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{} //Class used to handle unreadable file exceptions
$path = "D:\\in.txt";
try
{
file_open($path);
}
catch(FileExistsException $e) //If a FileExistsException occurs, the user will be 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 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 occurred
echo "Transmission route:";
print_r($e->getTrace()); //Return the tracking exception in the form of an array The route passed at each step
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 "an exception occurred during the operation of file_open function";
throw $e; //Rethrow exception
}
}
?>