<? Php Class FileExistsException extends Exception {} // Class used to handle non-abnormal File 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 system prompts the user 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 system prompts the user to check 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 User-Defined Exception Code Echo "file name:". $ e-> getFile (). "/n "; // Return the PHP program file name with an exception Echo "abnormal code row". $ e-> getLine (). "/n "; // Return the row number of the row where the code with an exception is located. Echo "route :"; Print_r ($ e-> getTrace ()); // Return the route passed by each step of the 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 } } ?> |