PHP Exception Handling implementation code

Source: Internet
Author: User
Tags php exception handling

CopyCode The Code is as follows: <? PHP
$ 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 );
}
}
?>Copy codeThe Code is as follows: <? PHP
$ 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 );
}
}
?>Copy codeThe Code is as follows: <? PHP
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 );
}
}
?>Copy codeThe Code is as follows: <? PHP
$ 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 PHPProgramFile Name
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 );
}
}
?>Copy codeThe Code is as follows: <? PHP
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

}
}
?>Copy codeThe Code is as follows: <? PHP
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 );
}
}
?>Copy codeThe Code is as follows: <? PHP
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")
{< br> throw new fileopenexception ("file cannot be opened", 2 );
}< BR >}< br> catch (exception $ e) // catch exceptions
{< br> echo "The file_open function encountered an exception while running ";
throw $ E; // throwing an exception
}< BR >?>

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.