Can you tell me how can I handle this kind of exceptions:
Fatal error: Uncaught exception 'Doctrine_Connection_Exception' with message 'PDO Connection Error: SQL开发者_如何学GoSTATE[HY000] [2013] Lost connection to MySQL server at 'reading initial communication packet', system error: 110' in ...
It happens when connection with MySQL is lost during query. I need to handle this exception so I can show 500 error page so the crawlers do not cache page, and to redirect user to appropriate "Try again" page.
P.S. I have a lot's of code, so I can not go trough all code to put try/catch block. I need something simple and yet effective.
I need something simple and yet effective
try/catch will be your only choice IMO. If you have to many places where you need to add a try/catch clause, you could move this particulat piece of code into a seperate function.
If, say, this line throws the execption
$con->query($your_DQL_query) //something happends here with the results
move it to an something like
function goodQuery($sql) {
try {
$con->query($sql);
} catch (Doctrine_Connection_Exception $dce) {
//do something
}
}
You still need to replace all of your "throwing" methods with a call to function goodQuery(...)
but this will make future development easier since the exception is handled in one place.
You don't handle it with Doctrine... You handle with your coding language. If you're using PHP:
try {
SomeClass::do_something(parameter);
} catch(DoctrineException $e) {
show_error($e->getMessage());
}
This is obviously sudo-code, but it gives you an idea... You could show a general error or be specific and display doctrine's message...
精彩评论