DjMikeS Posted October 3, 2008 Share Posted October 3, 2008 Hi all, I have this code to display a news item. As the news item is set in the url, users can change it to whatever the want resulting in an error because that news item may not exist. I'm trying to catch this error but for some reason it's not thrown. Can anyone help? Here's the code: <?php $cntBuffer = new Databuffer(); try { if (isset($_GET['item'])) { $newsId = mysql_real_escape_string($_GET['item']); $qryNews = "SELECT * FROM news WHERE id = $newsId"; if (!$rsltNews = mysql_query($qryNews)) { Throw new Exception("error"); } $arrNews = mysql_fetch_array($rsltNews); echo $arrNews[1]; } } catch (Exception $e) { $strError = $e->GetMessage(); echo "There is no such news item.<br /><i>$strError</i>"; } $cntBody = $cntBuffer->close(); ?> Link to comment https://forums.phpfreaks.com/topic/126874-solved-throwing-exceptions-not-working/ Share on other sites More sharing options...
JonnoTheDev Posted October 3, 2008 Share Posted October 3, 2008 Because the query is still valid! You want to throw your exception if there are no rows returned if (!mysql_num_rows($rsltNews)) { throw new Exception("error"); } Link to comment https://forums.phpfreaks.com/topic/126874-solved-throwing-exceptions-not-working/#findComment-656207 Share on other sites More sharing options...
DjMikeS Posted October 3, 2008 Author Share Posted October 3, 2008 Oh, lovely. Thnx! Link to comment https://forums.phpfreaks.com/topic/126874-solved-throwing-exceptions-not-working/#findComment-656209 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.