PHP exception and error handling

Source: Internet
Author: User

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 ){}
  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.

<? Phpclass OpenFileException extends Exception {function open () {touch ("tmp.txt"); $ file = fopen ("tmp.txt", "r"); return $ file ;}} class DemoException extends Exception {function pro () {echo "Demo Exception <br/>" ;}} 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 (). "<br/>"; $ file = $ e-> open ();} catch (DemoException $ e) {echo $ e-> getMessage (). "<br/>"; $ 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.

<? PHPerror_reporting (E_ALL); // 1. specify the error report. The error type is all // ini_set ("error_reporting", E_ALL); // temporarily change the value of the configuration file // ini_get ("upload_max_filesize "); // obtain the configuration file value ini_set ("display_errors", "off"); // 2. disable the Display Error ini_set ("log_errors", "on") in the configuration file; // 3. enable the error log function ini_set ("error_log", "C:/error. log "); // 4. error file (writable) error_log ("this is a error message !!!! "); // Output error message?>

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.