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:
- Class NotFoundException extends Exception {}
- Class InputException extends Exception {}
- Class DBException extends Exception {}
2. configure a processor that does not capture exceptions:
- Function exception_uncaught_handler (Exception $ e ){
- Header ('content-type: text/html; charset = utf-8 ');
- If ($ e instanceof NotFoundException)
- Exit ($ e->GetMessage ());
- Elseif ($ e instanceof DBException)
- Exit ($ e->GetMessage ());
- Else
- Exit ($ e->GetMessage ());
- }
- Set_exception_handler ('exception _ uncaught_handler ');
3. manually throw a DBException exception in the database connection code, but try... Catch:
- $ This->ResConn = mysql_connect ($ CONFIGS ['DB _ host'], $ CONFIGS ['DB _ user'], $ CONFIGS ['DB _ pwd']);
- If (false = is_resource ($ this->ResConn ))
- Throw new DBException ('database connection failed. '. Mysql_error ($ this->ResConn ));
4. Business logic:
- If (0! = Strcmp ($ curAlbum->Interest_id, $ it ))
- 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...