PHP exception and error handling _ PHP Tutorial

Source: Internet
Author: User
PHP exception and error handling. Exception handling: an exception occurs unexpectedly during the running of the program. The normal process of the script is changed with an exception. Try {} catch (exception object) {} if there is no problem with the code in try, it will handle the exception: an accident occurs unexpectedly during the program running, use exceptions to change the normal process of the script.

Try {} catch (exception object ){}
  1. If there is no problem with the code in try, execute the catch command after the code in try is executed.
  2. If an exception occurs in the code in try, an exception object is thrown and thrown to the object in catch, the code in try will not continue to be executed and will jump directly to catch for execution, the catch execution is complete and continues to be executed.

Note: the system prompts you what is going on. it is not the main task we do. we need to solve this exception in catch. if it cannot be solved, it will be thrown to the user.

 ";}} Class MyClass {function openfile () {$ file = @ fopen (" aaa.txt "," r "); if (! $ File) throw new OpenFileException ("file opening failed");} function demo ($ num = 0) {if ($ num = 1) throw new DemoException ("demo exception! ") ;}} Try {$ my = new MyClass (); $ my-> openfile (); $ my-> demo (" 1 ");} catch (OpenFileException $ e) {echo $ e-> getMessage ()."
"; $ File = $ e-> open ();} catch (DemoException $ e) {echo $ e-> getMessage ()."
"; $ E-> pro ();} catch (Exception $ e) {// receive all exceptions thrown but not processed echo $ e-> getMessage () ;}?>

Error handling: 1. Syntax error 2. runtime error 3. logic error

Error Report: Error: E_ERROR warning: E_WARNING note: E_NOTICE

Suggestion: in the development phase, all error reports are output, which facilitates debugging of the program. Running stage: Do not let the program output any error report.

 

Bytes. Try {} catch (exception object) {} if the code in try is correct, execute...

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.