This article describes the PHP exception handling method. Share to everyone for your reference. Specifically as follows:
<?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 file cannot be found, throw exception object
{
throw new Exception ("File not Found", 1);
}
if (!fopen ($path, "R"))
//If the file cannot be opened, throw the exception object
{
throw new Exception ("File Cannot open", 2);
}
? >
<?php
$path = "D://in.txt";
The path where the file is located
file_open ($path);
Call File_open function
File_open ($path)
{
if (!file_exists)
///If file cannot be found, throw exception object
{
throw new Exception ("File cannot be found", 1);
}
if (!fopen ($path, "R"))
//If the file cannot be opened, throw the exception object
{
throw new Exception ("File Cannot open", 2);
}
? >
<?php function Exception_handler ($e)//The exception that is used to handle the exception {echo is not caught: ". $e->getmessag
E ();
} set_exception_handler ("Exception_handler");
Set the exception handling function try//detect exception {$path = "d://in.txt";
The catch (Exception $e)//catch Exception {echo $e->getmessage (); } file_open ($path); Call function Open File function File_open ($path) {if (!file_exists ($path))//If file cannot be found, throw exception object {throw new Exception ("file None
Method found ", 1);
} if (!fopen ($path, "R"))//If the file cannot be opened, throw the exception object {throw new Exception ("File Cannot open", 2); }}?>
<?php $path = "D://in.txt"; try {file_open ($path);//attempt to open file} catch (Exception $e) {echo exception information:. $e->getmessage (). "
/n "; Returns the user-defined exception information echo "Exception code:". $e->getcode ().
/n "; Returns the user-defined exception code echo "FileName:". $e->getfile (). "
/n "; Returns the name of the PHP program file where the exception occurred. Echo "The line with exception code". $e->getline ().
/n ";
Returns the line number of the line where the exception occurred, echo "Routing:";
Print_r ($e->gettrace ());
Returns an array of trace exceptions every step of the route that is passed Echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string {File_open ($path) {if (!file_exists)//If the file does not exist, throw an error {throw new
Exception ("File cannot be found", 1);
} if (!fopen ($path, "R")) {throw new Exception ("File Cannot open", 2); }}?>
<?php class Fileexistsexception extends exception{}//classes for handling files that do not have an exception fileopenexception extends exception{}//For
Class $path for processing file unreadable exceptions = "d://in.txt";
try {file_open ($path); The catch (fileexistsexception $e)///If a Fileexistsexception exception is generated prompts the user to confirm that the file location {Echo program has an exception during Operation: ". $e->getmessage ()."
/n "; echo "Please confirm the file location.
"; The catch (fileopenexception $e)///If a Fileopenexception exception is generated prompts the user to confirm the readability of the file {echo "program has an exception during run:". $e->getmessage (). "
/n "; echo "Please confirm the readability of the file.
";
catch (Exception $e) {echo "[unknown exception]"; echo "Exception information:". $e->getmessage (). "
/n "; Returns the user-defined exception information echo "Exception code:". $e->getcode ().
/n "; Returns the user-defined exception code echo "FileName:". $e->getfile (). "
/n "; Returns the name of the PHP program file where the exception occurred. Echo "The line with exception code". $e->getline ().
/n ";
Returns the line number of the line where the exception occurred, echo "Routing:";
Print_r ($e->gettrace ());
Returns an array of trace exceptions every step of the route that is passed Echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string {File_open ($path) {if!file_exists ($path)) {throw new Fileexistsexcepti On ("File cannot be found", 1);
Throw Fileexistsexception Exception Object} if (!fopen ($path, "R")) {throw new Fileopenexception ("File Cannot open", 2);
Throw fileopenexception exception Object}}?>
<?php class Fileexistsexception extends exception{}//classes for handling files that do not have an exception fileopenexception extends exception{}//For
Class $path for processing file unreadable exceptions = "d://in.txt";
try {file_open ($path);//attempt to open file} catch (Exception $e) {echo [unknown exception]; echo "Exception information:". $e->getmessage (). "
/n "; Returns the user-defined exception information echo "Exception code:". $e->getcode ().
/n "; Returns the user-defined exception code echo "FileName:". $e->getfile (). "
/n "; Returns the name of the PHP program file where the exception occurred. Echo "The line with exception code". $e->getline ().
/n ";
Returns the line number of the line where the exception occurred, echo "Routing:";
Print_r ($e->gettrace ());
Returns an array of trace exceptions every step of the route that is passed Echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string (fileexistsexception $e)//If a fileexistsexception exception is generated, prompts the user to confirm that the file location {echo "program has changed during the run Often: ". $e->getmessage ()."
/n "; echo "Please confirm the file location.
"; The catch (fileopenexception $e)///If a Fileopenexception exception is generated prompts the user to confirm the readability of the file {echo "program has an exception during run:". $e->getmessage (). "
/n "; echo "Please confirm the readability of the file.
"; The function File_open ($path) {if (!file_exists ($path))//If the file does not exist, output error {throw newFileexistsexception ("File cannot be found", 1);
} if (!fopen ($path, "R")) {throw new Fileopenexception ("File Cannot open", 2);
}}?>
<?php class Fileexistsexception extends exception{}//classes for handling files that do not have an exception fileopenexception extends exception{}//For
Class $path for processing file unreadable exceptions = "d://in.txt";
try {file_open ($path); The catch (fileexistsexception $e)///If a Fileexistsexception exception is generated prompts the user to confirm that the file location {Echo program has an exception during Operation: ". $e->getmessage ()."
/n "; echo "Please confirm the file location.
"; The catch (fileopenexception $e)///If a Fileopenexception exception is generated prompts the user to confirm the readability of the file {echo "program has an exception during run:". $e->getmessage (). "
/n "; echo "Please confirm the readability of the file.
";
catch (Exception $e) {echo "[unknown exception]"; echo "Exception information:". $e->getmessage (). "
/n "; Returns the user-defined exception information echo "Exception code:". $e->getcode ().
/n "; Returns the user-defined exception code echo "FileName:". $e->getfile (). "
/n "; Returns the name of the PHP program file where the exception occurred. Echo "The line with exception code". $e->getline ().
/n ";
Returns the line number of the line where the exception occurred, echo "Routing:";
Print_r ($e->gettrace ());
Returns an array of trace exceptions every step of the route that is passed Echo $e->gettraceasstring (); Returns the Gettrace function information formatted as a string 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 open", 2);
The catch (Exception $e)//catch exception {echo File_open function is abnormal during run; Throw $e;
Throw exception}}?>
I hope this article will help you with your PHP programming.