(4) PHP object-oriented Theory 4 --- exception and error handling

Source: Internet
Author: User
: This article mainly introduces (4) PHP object-oriented Theory 4-exception and error handling. if you are interested in PHP tutorials, refer to it. I. exception handling:
1. PHP exception example:
 GetMessage (); $ a =-1;} echo $;
2. detailed exception example:
 Exception ($ this-> getCode (): {$ this-> getMessage ()} in File :( $ this-> getFile () on Line: {$ this-> getLine ()}

";}} Function reg ($ reginfo = null) {if (empty ($ reginfo) | isset ($ reginfo) {throw new Exception (" Invalid parameter ");} if (empty ($ reginfo ['email ']) {throw new emailException ("the email is blank");} if ($ reginfo ['pwd']! = $ Reginfo ['repwd']) {throw new pwdException ("two passwords inconsistent");} echo "registered successfully" ;}/ ** this code cannot be executed, find out the reason */try {reg (array ('email '=> 'waitfox @ qq.com', 'pwd' => 123456, 'repwd' => 12345678 ));} catch (emailException $ ee) {echo $ ee-> getMessage ();} catch (pwdException $ ep) {echo $ ep; echo PHP_EOL, "special processing ";} catch (Exception $ e) {echo $ e-> getTraceAsString (); echo PHP_EOL, "Unified processing in other cases ";}
3. exception handling scenarios:
A. pessimistic prediction of the program
B. program needs and business concerns
C. language-level Robustness Requirements: capture exceptions and remedy them accordingly
3. exception handling: The exception must be handled in a reasonable scenario.
II. PHP error levels:
1. PHP error level:
A. deprecated: minimum level. 'Not recommended, not recommend'
B. notice: incorrect syntax. For example, variables are used but not defined.
C. warning: function parameters do not match
D. fetal error: The PHP process is terminated, and subsequent code is not executed.
E. prase error: This error will be reported during the syntax check phase. The PHP Manual defines 16 levels of errors.
2. example:
 5) {echo "$ I not initialized", PHP_EOL;} $ a = array ('O' => 2, 4, 6, 8); echo $ a [o]; $ result = array_sum ($ a, 3); echo fun (); echo "error ...... Can I continue? ";
4. PHP error handling mechanism:
 Error code:[$ {Errno}], file {$ errstr} \ r \ n "; echo" error code line: {$ errline} File {$ errfile} \ r \ n "; echo" PHP version ", PHP_VERSION," (", PHP_ OS,") \ r \ n ";} set_error_handler ("customeError", E_ALL | E_STRICT); $ a = array ('O' => 2, 4, 6, 8); echo $ a ['O']; // no answer to this question. No error reported
III. PHP object-oriented summary:
PHP object-oriented knowledge points: Magic methods, interfaces, polymorphism, class multiplexing, reflection, and exception mechanisms.
PHP also introduces some functional programming concepts.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces (4) PHP object-oriented Theory 4-exception and error handling, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.