Follow the php[11]-php of the Baidu learn to deal with the exception

Source: Internet
Author: User
Tags apache log file getmessage php exception handling try catch apache log

The first thing to say about the common three kinds of errors:

1. Syntax errors

2. Run Error

3. Logic Error

00X1 Error Reporting and error levels

PHP errors are divided into three levels

1. Note (notice) no variable a

2. Warning (warning) No value is passed to the function

3. Fatal error (fatal error) function write error

The error reporting level can be modified in the php.ini.
Find Error Report "error_reporting = E_all" In php.ini This means to prompt all errors.
Ps:~ is besides the meaning. & is the meaning of and.
error_reporting = e_warning & e_fatal error (Warning warning and fatal error only) fatal error
error_reporting = E_all ~ e_notice (hint all, except note notice)

00X2 Custom PHP Report error handling

What is the handling of custom report errors?

That is

( ! ) Fatal error:call to undefined function geaaattype () inch D:\wamp\www\dnf.php on line 2

Write according to our intentions.

Obviously this error is PHP's own system has been brought in. Then we need to tell PHP that we don't want your own error function, I'm going to create one myself.

Set_error_handler ("Myerrorfun"); (handler transliteration: processing) This statement tells PHP that I want to create a myerrorfun error message myself. Then you need to define this function. The code might look like this:

<?  set_error_handler("Myerrorfun"); # registering a myerrorfun error function function myerrorfun ($type,$mess,$file,$line) {#  This function has four parameters, namely error type, error message, error file, error line  ?>

00X3 Setting the error log

There is an error in php.ini: Display_errors = On is on by default and we can turn it off. Apache log file: Log_errors = On

If you want to write Apache log files into the Windows system log, you can turn: Error_log = syslog on. The default is commented out. A semicolon is a comment.

00x4 PHP Exception handling (Try Catch)

If there is Trow new exception, then there is an exception.

1<?PHP2 Echo"Get up in the morning <br/>";3 Try{4 Echo"Drive to work." <br/> ";5 Throw New Exception("The car has a flat tire." <br/> ");//throws an exception, with the exception of the sixth line of Echo does not perform a direct execution catch6 Echo"The road is very good." ";7}Catch(Exception $e){8 Echo $e->getmessage ();//in fact, the $e here is abnormal. Exception is a class that the system has already defined, calling its GetMessage method. 9 Echo"Put on a spare tire and continue to drive to work." <br/> ";Ten } One?>

It would have been in order, 1-2-3-4-5-6 .... This is done, but when the program executes to throw new Exception (throws a fresh exception) it skips the sixth line to execute line 8th, and is assigned to $e,exception is actually a class, A system-defined class can be called GetMessage to get the "car burst" data in line 5th. Without the throw of the new exception, it would go straight down, saying that the traffic was good and that the 9th line of code would not be executed.

Then the question comes, how to know the system has been wrong???

idea: You can register an error function (00x2)and then use if to determine the type of error.

Case:

<?PHPSet_error_handler("Myerrorfun");functionMyerrorfun ($type,$mess,$file,$line){    if($type==e_warning) {#throws an exception if the error type is critical (that is, throw new Exception)        Throw New Exception("A serious error has occurred. {$mess},{$file},{$line}"); }}functionSay$language){#define a say function    Echo $language;#output this function directly. }say ();#intentionally wrong use of the function. ?>

The result is the following error:

( ! ) Fatal error:uncaught Exception ' exception ' with message ' There was a serious error. Missing Argument 1 for Say (), called in D:\wamp\www\test.php on line one and defined,d:\wamp\www\test.php,8 ' in D:\wamp\www \test.php on line 5
( ! ) Exception: A serious error has occurred. Missing Argument 1 for Say (), called in D:\wamp\www\test.php on line one and defined,d:\wamp\www\test.php,8 in D:\wamp\www\ test.php on line 5

Then the correct writing say function will not be an error.

<?PHPSet_error_handler("Myerrorfun");functionMyerrorfun ($type,$mess,$file,$line){    if($type==e_warning) {#throws an exception if the error type is critical (that is, throw new Exception)        Throw New Exception("A serious error has occurred. {$mess},{$file},{$line}"); }}functionSay$language){#define a say function    Echo $language;#output this function directly. }say ("Cherish the people who love you, do not wait for the loss to cherish." I will always be in love with you. ");#intentionally wrong use of the function. ?>

00X4 Custom Exception Classes

The previously learned exception is the system has already had the exception class, then we can customize the exception class? The answer is undoubtedly yes.

<?PHPclassMyExceptionextends Exception{#must inherit exception otherwise invalid.     functionSay () {#a function that defines a workaround.         EchoThe syntax for the say function is: Say (' strting ') "; }}Echo"11111111111<br/>";Try{    Echo"2222222222222<br/>"; Throw NewMyException ("syntax error. ");#throw syntax error calling catch    Echo"3333333333333<br/>";}Catch(myexception$e){    Echo $e->getmessage (). " <br/> ";#call the GetMessage method in the Myerrot class.     Echo $e->say (). " <br/> ";#Call the workaround in the MyException class. } ?>

The END

Follow the php[11]-php of the Baidu learn to deal with the exception

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.