PHP catch unhandled exceptions

Source: Internet
Author: User

How to catch unprocessed exceptions in PHP

Class MyException extends Exception {function MyException ($ message, Exception $ ex = NULL) {$ this-> message = $ message; $ this-> innerException = $ ex ;} public $ innerException = NULL;} function exception_handler ($ exception) {$ s = '<pre> '. var_export ($ exception, true ). '</pre>'; $ s = str_replace ("", "", $ s); echo "<B style = \" color: red; \ "> Uncaught exception </B>", $ s, "\ n"; error_log ($ s); // php. ini log_errors = On error_log = c:/javasdisplay_errors = On} set_exception_handler ('exception _ handler'); // error handler functionfunction customError ($ errno, $ errstr, $ errfile, $ errline) {echo 'mermerror <br> '; throw new ErrorException ($ errstr, 0, $ errno, $ errfile, $ errline);} // set error handlerset_error_handler ("customError "); function fnError ($ num, $ div) {return $ num/$ div;} $ fn = 'fnerror '; // $ I = $ fn (12 3, 0); try {$ I = 0; // throw new Exception ("error. ") ;}Catch (Exception $ e) {echo" error catch 1 "; throw new MyException (" custom error 1 ", $ e );}} catch (Exception $ e) {echo "error catch 2"; throw new MyException ("custom error 2", $ e );}

Other Tips

Clear buffer ob_clean (); clear all previous echo outputs.

Pause sleep for 3 seconds (3 );

Obtain the page execution duration:

$ Running_time = time ()-$ _ SERVER ['request _ time']; echo 'page running TIME: ', $ running_time,' seconds <br> ';

Constants in PHP

define("D1", "dddd1");class MyClass1{const APP_NAME = "app";}echo MyClass1::APP_NAME;echo D1;constant("D1");constant("MyClass1::APP_NAME");

PHP Session

session_start();if ( isset($_SESSION['SID']) ){echo $_SESSION['SID'];}$_SESSION["SID"] = "SESSION" . time();//session_destroy();//session_commit();

Set COOKIE setcookie ("tt", "cookie ");

Output variable content

$s = var_export($_REQUEST, TRUE);echo "<pre>$s</pre>";

  

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.