Php exception handling implementation code. Copy the code as follows :? Php $ pathD: \ in.txt; try to detect exceptions {file_open ($ path);} catch (Exception $ e) catch exceptions {echo $ e-getMessage ();} functionfile_open ($ path
The code is as follows:
$ Path = "D: \ in.txt ";
Try // detect exceptions
{
File_open ($ path );
}
Catch (Exception $ e) // catch an Exception
{
Echo $ e-> getMessage ();
}
Function file_open ($ path)
{
If (! File_exists ($ path) // if the file cannot be found, an exception object is thrown.
{
Throw new Exception ("the file cannot be found", 1 );
}
If (! Fopen ($ path, "r") // if the file cannot be opened, an exception is thrown.
{
Throw new Exception ("file cannot be opened", 2 );
}
}
?>
The code is as follows:
$ Path = "D: \ in.txt"; // file path
File_open ($ path); // call the file_open function
Function file_open ($ path)
{
If (! File_exists ($ path) // if the file cannot be found, an exception object is thrown.
{
Throw new Exception ("the file cannot be found", 1 );
}
If (! Fopen ($ path, "r") // if the file cannot be opened, an exception is thrown.
{
Throw new Exception ("file cannot be opened", 2 );
}
}
?>
The code is as follows:
Function exception_handler ($ e) // function used to handle exceptions
{
Echo "uncaptured exceptions:". $ e-> getMessage ();
}
Set_exception_handler ("exception_handler"); // sets the exception handling function.
Try // detect exceptions
{
$ Path = "D: \ in.txt ";
}
Catch (Exception $ e) // catch an Exception
{
Echo $ e-> getMessage ();
}
File_open ($ path); // call a function to open a file
Function file_open ($ path)
{
If (! File_exists ($ path) // if the file cannot be found, an exception object is thrown.
{
Throw new Exception ("the file cannot be found", 1 );
}
If (! Fopen ($ path, "r") // if the file cannot be opened, an exception is thrown.
{
Throw new Exception ("file cannot be opened", 2 );
}
}
?>
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 the custom exception code
Echo "File name:". $ e-> getFile (). "\ n"; // return the PHP program file name with an exception
Echo "row where the exception code is located". $ e-> getLine (). "\ n"; // return the row number of the row where the exception code is located.
Echo "route :";
Print_r ($ e-> getTrace (); // returns the path of each trace exception in the form of an array
Echo $ e-> getTraceAsString (); // returns the 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 ("the file cannot be found", 1 );
}
If (! Fopen ($ path, "r "))
{
Throw new Exception ("file cannot be opened", 2 );
}
}
?>
The code is as follows:
Class FileExistsException extends Exception {} // class used to handle non-existent file 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 exception occurs, the user is prompted to confirm the file location
{
Echo "An exception occurred when the program was running:". $ 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 when the program was running:". $ e-> getMessage (). "\ n ";
Echo "make sure the file is readable. ";
}
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 custom exception code
Echo "File name:". $ e-> getFile (). "\ n"; // return the PHP program file name with an exception
Echo "row where the exception code is located". $ e-> getLine (). "\ n"; // return the row number of the row where the exception code is located.
Echo "route :";
Print_r ($ e-> getTrace (); // returns the path of each trace exception in the form of an array
Echo $ e-> getTraceAsString (); // returns the getTrace function information formatted as a string.
}
Function file_open ($ path)
{
If (! File_exists ($ path ))
{
Throw new FileExistsException ("file cannot be found", 1); // throw a FileExistsException exception object
}
If (! Fopen ($ path, "r "))
{
Throw new FileOpenException ("file cannot be opened", 2); // throw a FileOpenException exception object
}
}
?>
The code is as follows:
Class FileExistsException extends Exception {} // class used to handle non-existent file 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 the custom exception code
Echo "File name:". $ e-> getFile (). "\ n"; // return the PHP program file name with an exception
Echo "row where the exception code is located". $ e-> getLine (). "\ n"; // return the row number of the row where the exception code is located.
Echo "route :";
Print_r ($ e-> getTrace (); // returns the path of each trace exception in the form of an array
Echo $ e-> getTraceAsString (); // returns the getTrace function information formatted as a string.
}
Catch (FileExistsException $ e) // if a FileExistsException exception occurs, the user is prompted to confirm the file location
{
Echo "An exception occurred when the program was running:". $ 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 when the program was running:". $ e-> getMessage (). "\ n ";
Echo "make sure the file is readable. ";
}
Function file_open ($ path)
{
If (! File_exists ($ path) // if the file does not exist, an error is returned.
{
Throw new FileExistsException ("file cannot be found", 1 );
}
If (! Fopen ($ path, "r "))
{
Throw new FileOpenException ("file cannot be opened", 2 );
}
}
?>
The code is as follows:
Class FileExistsException extends Exception {} // class used to handle non-existent file 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 exception occurs, the user is prompted to confirm the file location
{
Echo "An exception occurred when the program was running:". $ 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 when the program was running:". $ e-> getMessage (). "\ n ";
Echo "make sure the file is readable. ";
}
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 custom exception code
Echo "File name:". $ e-> getFile (). "\ n"; // return the PHP program file name with an exception
Echo "row where the exception code is located". $ e-> getLine (). "\ n"; // return the row number of the row where the exception code is located.
Echo "route :";
Print_r ($ e-> getTrace (); // returns the path of each trace exception in the form of an array
Echo $ e-> getTraceAsString (); // returns the getTrace function information formatted as 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 an Exception
{
Echo "the file_open function encountered an exception while running ";
Throw $ e; // throwing an exception
}
}
?>
The http://www.bkjia.com/PHPjc/319911.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/319911.htmlTechArticle code is as follows :? Php $ path = "D :\\ in.txt"; try // detect exceptions {file_open ($ path);} catch (Exception $ e) // capture exceptions {echo $ e-getMessage ();} function file_open ($ path...