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(); ?> Quote 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"); } Quote 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! Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.