Zend_Rss_Feed_Reader -> How to avoid exception if the feed source gets invalid?
I had the following:
$feedUrl = 'http://www.something.org/?feed=rss2';
$feed = Zend_Feed_Reader::import($feedUrl);
$lastNews = array();
//etc...
return $lastNews;
The problem was that, if the feed didn't exist for somereason, Zend will throw an exception and all my website will stay useless with that exception message.
I end up doing like this:
try {
$feedUrl = 'http://www.something.org/?feed=rss2';
$feed = Zend_Feed_Reader::import($feedUrl);
$lastNews = array();
//etc...
return $lastNews;
}
catch (Exception $e)
{
return false;
}
It works, but I just made up this. Not sure if 开发者_如何学Cit's ok. Any suggestions?
Regards, MEM
That essentially the way you handle an Exception. Im not sure if i would return false, my preference would probably be for an empty array
so that i dont have to have an if statement encapsulating loops that use the return value... but thats entirely up to you.
The only other exception would be to use a more specific exception like
try {
}
catch(Zend_Feed_Reader_Exception)
{
}
This way if a different error occurs you can handle it in a different fashion. Im not sure that exception actually exists but there is probably one or more exceptions unique tot he Zend_Feed component. Take a look at the code or docs to figure out which one(s) you want to catch and handle.
精彩评论