Php Exception Handling Method Instance summary and Handling Method Instance Summary

Source: Internet
Author: User

Php Exception Handling Method Instance summary and Handling Method Instance Summary

This article describes how to handle php exceptions. Share it with you for your reference. The details are 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) {if (! File_exists ($ path) // if the file cannot be found, an Exception object {throw new Exception ("file cannot be found", 1);} if (! Fopen ($ path, "r") // if the file cannot be opened, an Exception object {throw new Exception ("file cannot be opened", 2) is thrown; }}?>
<? Php $ path = "D: // in.txt"; // file path file_open ($ path); // call the file_open function file_open ($ path) {if (! File_exists ($ path) // if the file cannot be found, an Exception object {throw new Exception ("file cannot be found", 1);} if (! Fopen ($ path, "r") // if the file cannot be opened, an Exception object {throw new Exception ("file cannot be opened", 2) is thrown; }}?>
<? Php function exception_handler ($ e) // function used to handle exceptions {echo "uncaptured exceptions :". $ e-> getMessage ();} set_exception_handler ("exception_handler"); // set the exception handling function try // detect exceptions {$ path = "D: // in.txt ";} catch (Exception $ e) // catch exceptions {echo $ e-> getMessage ();} file_open ($ path); // call the function to open the file function file_open ($ path) {if (! File_exists ($ path) // if the file cannot be found, an Exception object {throw new Exception ("file cannot be found", 1);} if (! Fopen ($ path, "r") // if the file cannot be opened, an Exception object {throw new Exception ("file cannot be opened", 2) is thrown; }}?>
<? Php $ path = "D: // in.txt"; try {file_open ($ path); // try to open the file} catch (Exception $ e) {echo "Exception information :". $ e-> getMessage (). "/n"; // return the user-defined exception information echo "Exception Code :". $ e-> getCode (). "/n"; // return the User-Defined Exception Code echo "file name :". $ e-> getFile (). "/n"; // return the abnormal PHP program file name echo "abnormal code row ". $ e-> getLine (). "/n"; // return the line number of the Code in which an exception occurs echo "transfer route:"; print_r ($ e-> getTrace ()); // return the trace exception route echo $ e-> getTraceAsString () in an array; // return the getTrace function information formatted as a string} Function file_open ($ path) {if (! File_exists ($ path) // if the file does not exist, an error {throw new Exception ("the file cannot be found", 1);} if (! Fopen ($ path, "r") {throw new Exception ("file cannot be opened", 2) ;}}?>
<? Php class FileExistsException extends Exception {} // class FileOpenException extends Exception {} // class used to handle non-existent 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 "The program encountered an exception during 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" The program encountered an exception during running :". $ e-> getMessage (). "/n"; echo "Check the readability of the file. ";} Catch (Exception $ e) {echo" [unknown Exception] "; echo" Exception information :". $ e-> getMessage (). "/n"; // return the user-defined exception information echo "Exception Code :". $ e-> getCode (). "/n"; // return the User-Defined Exception Code echo "file name :". $ e-> getFile (). "/n"; // return the abnormal PHP program file name echo "abnormal code row ". $ e-> getLine (). "/n"; // return the line number of the Code in which an exception occurs echo "transfer route:"; print_r ($ e-> getTrace ()); // return the trace exception route echo $ e-> getTraceAsString () in an array; // return the getTrace function information formatted as a string} function file_open ($ path) {if (! File_exists ($ path) {throw new FileExistsException ("file not found", 1); // throw FileExistsException exception object} if (! Fopen ($ path, "r") {throw new FileOpenException ("file cannot be opened", 2); // throw a FileOpenException exception object}?>
<? Php class FileExistsException extends Exception {} // class FileOpenException extends Exception {} // class used to handle non-existent 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 the user-defined exception information echo "Exception Code :". $ e-> getCode (). "/n"; // return the User-Defined Exception Code echo "file name :". $ e-> getFile (). "/n"; // return the abnormal PHP program file name echo "abnormal code row ". $ e-> getLine (). "/n "; // Return the line number of the row where the code with an exception is located echo "transfer route:"; print_r ($ e-> getTrace ()); // return the trace exception route echo $ e-> getTraceAsString () in an array; // return 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 "The program encountered an exception during 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" The program encountered an exception during running :". $ e-> getMessage (). "/n"; echo "Check the readability of the file. ";} Function file_open ($ path) {if (! File_exists ($ path) // if the file does not exist, the output error {throw new FileExistsException ("file cannot be found", 1);} if (! Fopen ($ path, "r") {throw new FileOpenException ("file cannot be opened", 2) ;}}?>
<? Php class FileExistsException extends Exception {} // class FileOpenException extends Exception {} // class used to handle non-existent 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 "The program encountered an exception during 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" The program encountered an exception during running :". $ e-> getMessage (). "/n"; echo "Check the readability of the file. ";} Catch (Exception $ e) {echo" [unknown Exception] "; echo" Exception information :". $ e-> getMessage (). "/n"; // return the user-defined exception information echo "Exception Code :". $ e-> getCode (). "/n"; // return the User-Defined Exception Code echo "file name :". $ e-> getFile (). "/n"; // return the abnormal PHP program file name echo "abnormal code row ". $ e-> getLine (). "/n"; // return the line number of the Code in which an exception occurs echo "transfer route:"; print_r ($ e-> getTrace ()); // return the trace exception route echo $ e-> getTraceAsString () in an array; // return the getTrace function information formatted as a string} function file_open ($ path) {try {if (! File_exists ($ path) {throw new FileExistsException ("file not found", 1);} if (! Fopen ($ path, "r") {throw new FileOpenException ("file cannot be opened", 2) ;}} catch (Exception $ e) // catch exceptions {echo "exception occurred when file_open function is running"; throw $ e; // throwing exception}?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.