Handling exceptions in
The following 4 pieces of code are my simple applications in the waylife project (non-production environment). They are not robust or beautiful, but the SNS project Already died in infancy.
1. Hierarchical relationship of exception classes:
- class NotFoundException extends Exception{}
- class InputException extends Exception{}
- class DBException extends Exception{}
2. Configure the handler for uncaught 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. In the database Connect the code and manually throw the DBException exception but do not use try...catch to catch it. The exception will be handled by the PHP custom exception handler exception_uncaught_handler() function:
- $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 at a glance:
- if (0 != strcmp($curAlbum->interest_id, $it))
- throw new NotFoundException('Sorry, The photo album you visited does not exist');
The above is the specific use of PHP custom exception handler.
http://www.bkjia.com/PHPjc/446148.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446148.htmlTechArticleHandling exceptions in the following 4 pieces of code is my simple application in the waylife project (non-production environment), which is not robust Not to beautify it, but the SNS project has long since died. 1. Hierarchical relationship of exception classes...