How does php capture exceptions? -Php Tutorial

Source: Internet
Author: User
How does php capture exceptions? How does php capture exceptions?
Is there a mechanism similar to. net try catch?
Php cannot be debugged line by line!


Reply to discussion (solution)

Some are written in the same way.
Try {

} Catch (Exception $ e ){
Print $ e-> getMessage ();

}

Is there a mechanism similar to. net try catch? This really is ~
Php cannot be debugged line by line! The landlord did not find the correct ide

In fact, php debugging can be adjusted at any time. the simplest way is to output the code at any time. Of course, zend-studio also provides breakpoint function debugging, but it is far less convenient.
In fact, each has its own advantages, brother.

#1 cute polar bear

Try {$ a = "sgfdfgdf"; $ B = (int) $ a; echo ($ B);} catch (exception $ e) {echo ("error ". $ e-> getMessage (); // The message is executed here}

You also need to throw the Exception throw new exception ('It is an Exception! '), And then captured by catch (Exception $ e.

You also need to throw the Exception throw new exception ('It is an Exception! '), And then captured by catch (Exception $ e. \
This can be captured, but I still need to specify the error information.
All I need is an error message. I know that I have caught a ghost.

An exception occurred when we used echo to locate the problem. php couldn't.

Let's look at php's error_log... there will be various detailed error messages... what else can catch...

Let's look at php's error_log... there will be various detailed error messages... what else can catch...
Where does php error_log look?

There are two types of errors. one is something php can capture, such as a syntax error. Another kind of error is a logical error. this error cannot be captured by machines, so try catch is used for human capture.
I think the error you mentioned is the previous one. you can see the error prompt display_errors = On.

There is also set_exception_handle.

Your code is normal. of course you will not execute that sentence.

PHP code

Try {
$ A = "sgfdfgdf ";
$ B = (int) $;
Echo ($ B );
}
Catch (exception $ e ){
Echo ("error occurred". $ e-> getMessage (); // The message is executed here.
}

Your code is normal. of course you will not execute that sentence.

Reply by referencing telankes2000 on the fifth floor:

PHP code

Try {
$ A = "sgfdfgdf ";
$ B = (int) $;
Echo ($ B );
}
Catch (exception $ e ){
Echo ("error occurred". $ e-> getMessage (); // The message is executed here.
}
Incorrect integer conversion. this is not an error.

This error should be automatically captured by the system.

Ke, this is not an error. in php, automatic conversion

Reference the reply from helloyou0 on the 14th floor:

Your code is normal. of course you will not execute that sentence.

Reply by referencing telankes2000 on the fifth floor:

PHP code

Try {
$ A = "sgfdfgdf ";
$ B = (int) $;
Echo ($ B );
}
Catch (exception $ e ){
Echo ("error occurred". $ e-> getMessage (); // The message is executed here.
......

Incorrect integer conversion. in any language, forced type conversion is not an error!
Although sometimes you don't get the expected results

Php error exceptions are similar to java errors, but they must be thrown by yourself: example:
Try {
$ M = 0;
If ($ m <1 ){
Throw new Exception ('m ='. $ m. ': Less Than Zero ');
}
Echo $ m;
} Catch (exception $ e ){
Echo "error [line->". ($ e-> getLine (). "]:". ($ e-> getMessage ());
}

Php weak type changes data type no problem

If the divisor is set to 0, an exception is thrown.

I have the same questions.
For example, if $ _ GET is used in php, but no get parameter is input, an error occurs. I cannot catch this error using try catch.
How can I know how to handle get errors?

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.