Php error and Exception Handling Mechanism (Supplement)

Source: Internet
Author: User
Tags php error

I. Error Handling
Exception Handling: unexpected, unexpected, occurs during the running of the program. Use an exception to change the normal process of the script.
A new important feature in PHP5
Copy codeThe Code is as follows:
If (){
} Else {
}
Try {
} Catch (exception object ){
}

1. If there is no problem with the code in try, it will be executed after the code in try is executed.
2. If an exception occurs in the try code, an exception object (throw) is thrown to the catch parameter, and the code in try will not continue to be executed.
Directly jump to the catch to execute, the catch is completed, and then continue to execute downward
Note: The system prompts the exception. This is not the main task. We need to solve this exception in catch. If the exception cannot be solved, we will go out to the user.
2. Define an exception class
Purpose: write one or more methods to solve the problem.
1. Define the Exception class by yourself. It must be a subclass of the Exception (built-in class,
2. In the Exception class, only the constructor and toString () can be rewritten, and the rest are final.
3. handle multiple exceptions
If an exception is thrown in the method when you define a function category
Copy codeThe Code is as follows:
Class OpenFileException extends Exception {
Function _ construct ($ message = null, $ code = 0 ){
Parent: :__ construct ($ message, $ code );
Echo "wwwwwwwwwwwwwww <br> ";
}
Function open (){
Touch ("tmp.txt ");
$ File = fopen ("tmp.txt", "r ");
Return $ file;
}
}
Class DemoException extends Exception {
Function pro (){
Echo "handling demo exceptions <br> ";
}
}
Class TestException extends Exception {
Function pro (){
Echo "Here the exception of test is handled <br> ";
}
}
Class HelloException extends Exception {
}
Class MyClass {
Function openfile (){
$ File = @ fopen ("tmp.txt", "r ");
If (! $ File)
Throw new OpenFileException ("file opening failed ");
}
Function demo ($ num = 0 ){
If ($ num = 1)
Throw new DemoException ("exception demonstrated ");
}
Function test ($ num = 0 ){
If ($ num = 1)
Throw new TestException ("test error ");
}
Function fun ($ num = 0 ){
If ($ num = 1)
Throw new HelloException ("###########");
}
}
Try {
Echo "11111111111111 <br> ";
$ My = new MyClass ();
$ My-> openfile ();
$ My-> demo (0 );
$ My-> test (0 );
$ My-> fun (1 );
Echo "22222222222222222 <br> ";
} Catch (OpenFileException $ e) {// $ e = new Exception ();
Echo $ e-> getMessage (). "<br> ";
$ File = $ e-> open ();
} Catch (DemoException $ e ){
Echo $ e-> getMessage (). "<br> ";
$ E-> pro ();
} Catch (TestException $ e ){
Echo $ e-> getMessage (). "<br> ";
$ E-> pro ();
} Catch (Exception $ e ){
Echo $ e-> getMessage (). "<br> ";
}
Var_dump ($ file );
Echo "444444444444444444444 <br> ";

Related Article

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.