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 ){}
- If there is no problem with the code in try, execute the catch command after the code in try is executed.
- 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...