PHP exception details

Source: Internet
Author: User
Tags php exception handling try catch

PHP exception handling is probably based on Java's exception handling mechanism.

In PHP, try, catch, and throw are used for exception handling.

In PHP, try is used to detect exceptions, catch is used to catch exceptions, and throw is used to throw exceptions.

What is the principle of PHP exception handling?

In the PHP program, the program starts execution and runs to try to check for exceptions. If an exception exists and throws an exception (throw), the PHP program catches the exception through catch, handle exceptions.

If there is no exception or no exception is thrown, continue to execute the remaining programs until the program is executed.

Bytes -----------------------------------------------------------------------------------

How does PHP handle exceptions?

After setting the exception object in PHP, throw is required to throw the exception.

An exception is thrown using the PHP keyword. The syntax is as follows:

Throw $ E;

$ E is an exception object created using exception.

In PHP, an exception object is usually created when an exception is thrown to optimize the Code. For example:

Throw new exception ([String $ errmsg])

For example:

<? PHP
$ Path = "d :\\ www.phpdo.net ";
File_open ($ PATH );
Function file_open ($ PATH)
{
If ($ PATH)
{
Echo "file exists ";
}
Else if (! File_extst ($ PATH ))
{
Throw new exception ("the file does not exist", 1 );
}
Else if (! Fopen ($ path, "R "))
{
Throw new exception ("file cannot be opened", 2 );
}
}
?>

The result is as follows:

Fatal error: uncaught exception 'exception' with message 'WWW .phpdo.net this file does not exist in 'in E: \ XAMPP \ htdocs \ PHP \ test \ 10.2.3.php: 9 stack trace: #0 E: \ XAMPP \ htdocs \ PHP \ test \ 10.2.3.php (3): file_open ('e: \ www. phpdo. ne... ') #1 {main} thrown in E: \ XAMPP \ htdocs \ PHP \ test \ 10.2.3.php on line 9

--------------------------------------------------------------------------------
PHP exception class exception
Posted 540 days ago: php_basic syntax comment 2

In the exception handling program in PHP, a try must correspond to one or more throws. The sequence defined in throw is the exception detection sequence.

When throw is used to throw an exception, you must use the PHP exception class exception.

What is the definition of the exception class?

The Code is as follows:

<? PHP
Class exception
{
Protected $ message = 'unknow exception'; // custom exception information
Protected $ code = 0; // defined Exception Code
Protected $ file; // name of the PHP program with an exception
Protected $ line; // The PHP line number with an exception
// Constructor used to pass user-defined exception information and user-defined Exception Code
Function _ construct ($ message = NULL, $ code = 0 );
Final function getmessage ();
Final function getcode ();
Final function GetFile ();
Final function Getline ();
Final function gettrace (); // returns the route for passing an exception in array format
Final function gettraceasstring (); // returns the gettrace function information formatted as a string.
Function _ tostring (); // reload-able, used to return output strings
}
?>

Exception object created by exception is used to store exception information and to throw and capture exceptions.

The syntax format for creating an exception object is as follows:

$ E = new exception ([String $ errmsg [, int $ errcode]);

$ Errmsg is the user-defined exception information. $ errcode is the user-defined code.

For example:

<? PHP
$ E = new exception ("www.phpdo.net error", 23 );
Echo $ e-> getmessage ();
Echo $ e-> getcode ();
Echo $ e-> GetFile ();
Echo $ e-> Getline ();
?>

Result:

Www.phpdo.net Error

23

E: \ XAMPP \ htdocs \ PHP \ test \ 10.2.2.php

2

-------------------------------------------------------------------------------

How to capture exceptions in PHP
Posted 535 days ago, without comments on the basic syntax of php‑basic syntax

After throw an exception with the keyword throw of PHP, catch is required to catch the exception.

Catch in PHP must be used together with try. The syntax is as follows:

Try {

// Statements that may cause exceptions

}

Catch (exception $ e ){

Operations after capturing exceptions

}

The following is an example of phpdo:

<? PHP
$ Path = "e :\\ www.phpdo.net ";
Try
{
File_open ($ PATH );
}
Catch (exception $ E)
{
Echo $ e-> getmessage ();
}
Function file_open ($ PATH)
{
If (! File_exists ($ PATH ))
{
Throw new exception ("the file www.phpdo.net does not exist", 1 );
}
If (! Fopen ($ path, "R "))
{
Throw new exception ("the file www.phpdo.net cannot be opened", 2 );
}
}
?>

Result:

The file www.phpdo.net does not exist.

--------------------------------------------------------------------------
PHP functions that handle exceptions
Posted 534 days ago, without comments on the basic syntax of php‑basic syntax

When using PHP, it is impossible to use the check function try for all exceptions. Therefore, in actual applications, PHP handles abnormal functions and uses this special function to handle exceptions. If an exception is thrown but not captured (it generally means that the try catch is not used and throw is directly used to throw the exception. This exception is not caught.) the UDF is automatically called.

Use the set_exception_handler function in PHP to set this exception function. The syntax is as follows:

Set_exception_handler (exception_handler)

Exception_handler is the name of the function used to handle exceptions not captured.

The syntax of this exception handling function is as follows:

Function exception_handler ($ e ){}

$ E is an exception object.

It is worth noting that the definition of this function must be before the set_exception_handler function.

For example:

<? PHP
Function exception_handler ($ E)
{// Function used to handle exceptions
Echo "uncaptured exceptions:". $ e-> getmessage ();
}
Set_exception_handler ("exception_handler ");
Try
{
$ Path = "www.phpdo.net ";
}
Catch (exception $ E)
{
Echo $ e-> getmessage ();
}
File_open ($ PATH); // This function is not detected
Function file_open ($ PATH)
{
If (! File_exists ($ PATH ))
{
Throw new exception ("the file www.phpdo.net does not exist", 1 );
}
If (! Fopen ($ path, "R "))
{
Throw new exception ("the file www.phpdo.net cannot be opened", 2 );
}
}
?>

Result:

Uncaptured exception: the file www.phpdo.net does not exist

-------------------------------------------------

Gettrace of PHP
Posted 533 days ago, without comments on the basic syntax of php‑basic syntax

Every PHP programmer wants to track the exception transfer route. The gettrace function of PHP implements this function in the form of an array.
The gettrace function of PHP mainly includes the following key values:

Fiel: name of the PHP program file in which an exception occurs;
Line: The line number of the Code where an exception occurs;
Function: a function or method in which an exception occurs;
Class: the class of the function or method in which an exception occurs;
Type: the type of the function or method that calls an exception (":" Calls a static class member, "->" calls an instantiated object );
ARGs: The parameter accepted by the function or method in which an exception occurs.

For example:

<? PHP
$ Path = "www.phpdo.net ";
Try
{
File_open ($ PATH );
}
Catch (exception $ E)
{
Echo "exception information". $ e-> getmessage (). "\ n ";
Echo "Exception Code". $ e-> getcode (). "\ n ";
Echo "abnormal File". $ e-> GetFile (). "\ n ";
Echo "abnormal code row". $ e-> Getline (). "\ n ";
Echo "route :";
Print_r ($ e-> gettrace (); // returns the route of each part 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)
{
If (! File_exists ($ PATH ))
{
Throw new exception ("the file www.phpdo.net does not exist", 1 );
}
If (! Fopen ($ path, "R "))
{
Throw new exception ("the file www.phpdo.net cannot be opened", 2 );
}
}
?>

Result:

Exception information www.phpdo.net this file does not exist Exception Code 1 exception file E: \ XAMPP \ htdocs \ PHP \ test \ 10.2.6.php Exception Code line 22

Route:

Array ([0] => array (=> E: \ XAMPP \ htdocs \ PHP \ test \ 10.2.6.php [Line] => 5 [function] => file_open [ARGs] => array ([0] => www.phpdo.net )))

#0 E: \ XAMPP \ htdocs \ PHP \ test \ 10.2.6.php (5): file_open ('www .phpdo.net ') #1 {main}

--------------------------------------------------------

PHP extension exception handling class
Posted 531 days ago, without comments on the basic syntax of php‑basic syntax

During PHP programming, although exceptions can be tracked, different exception handling classes must be used in practical applications to handle different exceptions.

Therefore, the PHP extension exception handling class is generated.

For example:

<? PHP
Class fileexists extends exception
{
// File does not exist exception
}
Class fileopen extends exception
{
// The file cannot be opened. An exception occurs.
}
$ Path = "E: // www.phpdo.net ";
Try
{
Open_file ($ PATH );
}
Catch (fileexists $ E)
{
Echo "program running exception:". $ e-> getmessage (). "\ n ";
}
Catch (fileopen $ E)
{
Echo "program running exception,". $ e-> getmessage (). "\ n ";
}
Catch (exception $ E)
{
Echo "unpredictable exceptions ";
Echo "exception information:". $ e-> getmessage (). "\ n ";
Echo "Exception Code:". $ e-> getcode (). "\ n ";
Echo "file name:". $ e-> GetFile (). "\ n ";
Echo "the row where the Exception Code is located". $ e-> Getline (). "\ n ";
Echo "exception information transmission route :";
Print_r ($ e-> gettrace (); // return the transfer route in the array format
Echo $ e-> gettraceasstring (); // return the transfer route in the string format.
}

Function open_file ($ PATH)
{
If (! File_exists ($ PATH ))
{
// Throw a fileexists exception
Throw new fileexists ("www.phpdo.net does not exist", 1 );
}
If (! Fopen ("$ path", R ))
{
// Throw a fileopen exception
Throw new fileopen ("www.phpdo.net cannot be opened", 2 );
}
}
?>

Result:

Program running exception: http://www.phpdo.net/does not exist

Note the following when handling PHP extension exceptions:

When capturing a specific exception, you also need to capture the exception class to handle uncaptured exceptions;
When an exception is caught, it needs to be captured from the top down in order (if the exception class is caught first, the exception cannot be processed by the correct code, for example, "catch (exception $ E) what will happen if the statement is placed before "catch (fileexists $ E ?) Therefore, when you set the catch statement for a specific exception, you must write the general exception statement at the end.

Based on this example, we can see what are the advantages of the exception handling class with PHP extension?

Improve code readability, so that different exceptions are easily differentiated;

Customizable extension classes;
Different responses are made based on different exceptions.
Exceptions can be passed up one level until they are captured. You don't have to worry about the exceptions thrown inside a function. How can you receive values in the outer layer?

-----------------------------------------------------

How PHP handles exception transfer and resetting
Posted 530 days ago, without comments on the basic syntax of php‑basic syntax

Sometimes in the actual application of PHP, we want to keep this exception, pass the exception to the previous layer of code, and handle it at the appropriate time.

How does PHP handle exception transfer and resetting?

In PHP, an exception is thrown again by calling the catch Statement (returning the handling method of the thrown exception object to the current method), so that the exception is passed up.

For example:

<? PHP
Class fileexists extends exception
{
// File does not exist exception
}
Class fileopen extends exception
{
// The file cannot be opened. An exception occurs.
}
$ Path = "E: // www.phpdo.net ";
Try
{
Open_file ($ PATH );
}
Catch (fileexists $ E)
{
Echo "program running exception:". $ e-> getmessage (). "\ n ";
}
Catch (fileopen $ E)
{
Echo "program running exception,". $ e-> getmessage (). "\ n ";
}
Catch (exception $ E)
{
Echo "unpredictable exceptions ";
Echo "exception information:". $ e-> getmessage (). "\ n ";
Echo "Exception Code:". $ e-> getcode (). "\ n ";
Echo "file name:". $ e-> GetFile (). "\ n ";
Echo "the row where the Exception Code is located". $ e-> Getline (). "\ n ";
Echo "exception information transmission route :";
Print_r ($ e-> gettrace (); // return the transfer route in the array format
Echo $ e-> gettraceasstring (); // return the transfer route in the string format.
}

Function open_file ($ PATH)
{
Try
{
If (! File_exists ($ PATH ))
{
// Throw a fileexists exception
Throw new fileexists ("www.phpdo.net does not exist", 1 );
}
If (! Fopen ($ path, R ))
{
// Throw a fileopen exception
Throw new fileopen ("www.phpdo.net cannot be opened", 2 );
}
}
Catch (exception $ E)
{
Echo "open_file exception ";
Throw $ E; // throw an exception object to facilitate catch (fileexists $ E), catch (fileopen $ E), and catch (exception $ e) calls.
}
}
?>

Result:

Open_file has an exception.

Program running exception: www.phpdo.net does not exist

----------------------------------------------------------------------------------
Exception class
The exception class is the root class of all exceptions. We can inherit it to build our own exception classes. The constructor of the exception class can have two optional parameters. One error message is used to describe the exception class and one error code.
There are some useful methods for exception classes:
Getmessage ();
Getcode ();
GetFile (); which file has an error?
Getline (); the row of the Error
Gettrace (); stack trajectory
Gettraceasstring ();
_ Tostring ();

 

 

Source: http://hwp2011.blog.51cto.com/3874564/710372

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.