Handle exceptions in
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.