PHP custom exception processor usage _ PHP Tutorial

Source: Internet
Author: User
PHP custom exception processor usage methods. The following four sections of code handle exceptions are simple applications (non-production environments) in my waylife project, which are neither robust nor beautifying, but the SNS project has been aborted. 1. an exception occurs when the hierarchical relationship processing of the exception class is

The following four sections of code show my simple application (non-production environment) in the waylife project, which is neither robust nor beautifying, but the SNS project has died.

1. hierarchical relationship of exception classes:

  1. Class NotFoundException extends Exception {}
  2. Class InputException extends Exception {}
  3. Class DBException extends Exception {}


2. configure a processor that does not capture exceptions:

  1. Function exception_uncaught_handler (Exception $ e ){
  2. Header ('content-type: text/html; charset = utf-8 ');
  3. If ($ e instanceof NotFoundException)
  4. Exit ($ e->GetMessage ());
  5. Elseif ($ e instanceof DBException)
  6. Exit ($ e->GetMessage ());
  7. Else
  8. Exit ($ e->GetMessage ());
  9. }
  10. Set_exception_handler ('exception _ uncaught_handler ');

3. manually throw a DBException exception in the database connection code, but try... Catch:

  1. $ This->ResConn = mysql_connect ($ CONFIGS ['DB _ host'], $ CONFIGS ['DB _ user'], $ CONFIGS ['DB _ pwd']);
  2. If (false = is_resource ($ this->ResConn ))
  3. Throw new DBException ('database connection failed. '. Mysql_error ($ this->ResConn ));


4. Business logic:

  1. If (0! = Strcmp ($ curAlbum->Interest_id, $ it ))
  2. Throw new NotFoundException ('sorry, the album you accessed does not exist ');

The above describes how to use a PHP custom exception processor.


The following four pieces of code of idea are simple applications (non-production environment) in my waylife project, which are neither robust nor beautifying, but the SNS project has died. 1. hierarchical relationship of exception classes...

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.