Copy code The code is as follows:
$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 cannot be opened", 2);
}
}
?>
Copy code The code is as follows:
$path = "D:\in.txt"; //The path to the file
file_open($path); //Call the file_open function
function file_open($path)
{
if(!file_exists($path)) //If the file cannot be found, throw Exception object
{
throw new Exception("File cannot be found", 1);
}
if(!fopen($path, "r")) //If the file cannot be found Open, throw exception object
{
throw new Exception("File cannot be opened", 2);
}
}
?>
Copy code The code is as follows:
function exception_handler($e) //Used to handle exceptions Function
{
echo "uncaught exception:".$e->getMessage();
}
set_exception_handler("exception_handler"); //Set the exception handling function
try //Detect exception
{
$path = "D:\in.txt";
}
catch(Exception $e) //Catch exception
{
echo $e->getMessage();
}
file_open($path); //Call function to open file
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);
}
}
?>
Copy code The code is as follows:
$path = "D:\in.txt";
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 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 code is located
echo "Transfer route:";
print_r($e->getTrace()); //Return the route passed at 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 exist, then Throw error
{
throw new Exception("File cannot be found", 1);
}
if(!fopen($path, "r"))
{
throw new Exception("File cannot be opened", 2);
}
}
?>
Copy code The code is as follows:
class FileExistsException extends Exception{} //Class used to handle file non-existence exceptions
class FileOpenException extends Exception{} //Class used to handle file unreadable exceptions
$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 "The program is running An exception occurred: ".$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 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 " Pass route: ";
print_r($e->getTrace()); //Return the route passed at 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 exception object
}
if(!fopen($path, "r"))
{
throw new FileOpenException( "File cannot be opened", 2); //Throws FileOpenException exception object
}
}
?>
Copy code The code is as follows:
class FileExistsException extends Exception{} //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); //Try to open 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 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 occurred
echo "Transfer route:";
print_r($e->getTrace()); //With Returns the route of each step of the tracking exception in array form
echo $e->getTraceAsString(); //Returns the getTrace function information formatted as a string
}
catch(FileExistsException $e) //If 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. ";
}
function file_open($path)
{
if(!file_exists($path)) //If the file does not exist, output an error
{
throw new FileExistsException("File cannot be found", 1);
}
if(!fopen($path, "r"))
{
throw new FileOpenException("File Unable to open", 2);
}
}
?>
Copy code The code is as follows:
class FileExistsException extends Exception{} //Class used to handle file non-existence exceptions
class FileOpenException extends Exception{} //Class used to handle file unreadable exceptions
$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 "The program is running An exception occurred: ".$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 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 " Pass route: ";
print_r($e->getTrace()); //Return the route passed at 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 ("The file cannot be opened", 2);
}
}
catch(Exception $e) //Catch the exception
{
echo "An exception occurred during the operation of the file_open function";
throw $e; //re-throw exception
}
}
?>
http://www.bkjia.com/PHPjc/319911.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319911.htmlTechArticleCopy the code as follows: ?php $path = "D:\in.txt"; try //Detect exceptions { file_open($path); } catch(Exception $e) //Catch exception { echo $e-getMessage(); } function file_open($path...