sneskid Posted January 25, 2007 Share Posted January 25, 2007 in Javascript one can easily do the following:<script type="text/javascript">try {eval('will cause error');}catch(e) {alert(e);}</script>in PHP the try catch system is only for errors thrown by your code, seems like I can't catch errors thrown by PHP.<?phptry {eval('will cause error');}catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n";}?>Sadly that wont work, really limits the whole try/catch concept.Any other ways I can achieve a Javascript like functionality? Quote Link to comment https://forums.phpfreaks.com/topic/35738-try-catch/ Share on other sites More sharing options...
konnwat Posted January 25, 2007 Share Posted January 25, 2007 [code]<?phptry {eval('will cause error');}catch (Exception $e) { echo 'Caught exception: '.$e->getMessage()."\n";}?>[/code]try that ^^ Quote Link to comment https://forums.phpfreaks.com/topic/35738-try-catch/#findComment-169349 Share on other sites More sharing options...
.josh Posted January 25, 2007 Share Posted January 25, 2007 please note that the [url=http://us3.php.net/manual/en/language.exceptions.php]try..catch[/url] is for php5. It won't work in php4.You might want to get creative with the [url=http://www.php.net/die]die[/url] statement, like [code]$sql = "select something from table";$blah = mysql_query($sql) or die(mysql_error());// will run the query. if it returns a fail (false, the error that mysql encountered will be displayed[/code] Quote Link to comment https://forums.phpfreaks.com/topic/35738-try-catch/#findComment-169355 Share on other sites More sharing options...
sneskid Posted January 26, 2007 Author Share Posted January 26, 2007 [quote author=konnwat link=topic=124065.msg513580#msg513580 date=1169764776][code]<?phptry {eval('will cause error');}catch (Exception $e) { echo 'Caught exception: '.$e->getMessage()."\n";}?>[/code]try that ^^[/quote]yea, that doesn't work. it won't execute anything inside the catch block, but still raises the parse error, i want to catch that error.im using php 5.1.4 Quote Link to comment https://forums.phpfreaks.com/topic/35738-try-catch/#findComment-169427 Share on other sites More sharing options...
trq Posted January 26, 2007 Share Posted January 26, 2007 You cant catch parse errors. In fact you can't catch php generated errors.The best you could do would be.....[code]<?phptry { if (!@eval('will cause error')) { throw new exception('unknown error'); }} catch (Exception $e) { echo 'Caught exception: '.$e->getMessage()."\n";}?>[/code]Which I'm sure is not really what you want to hear. Ive always thought the same myself, not being able to catch php's exceptions.... then whats the point? Quote Link to comment https://forums.phpfreaks.com/topic/35738-try-catch/#findComment-169433 Share on other sites More sharing options...
sneskid Posted January 26, 2007 Author Share Posted January 26, 2007 [quote author=thorpe link=topic=124065.msg513664#msg513664 date=1169773177]You cant catch parse errors. In fact you can't catch php generated errors.The best you could do would be.....[code]<?phptry { if (!@eval('will cause error')) { throw new exception('unknown error'); }} catch (Exception $e) { echo 'Caught exception: '.$e->getMessage()."\n";}?>[/code]Which I'm sure is not really what you want to hear. Ive always thought the same myself, not being able to catch php's exceptions.... then whats the point?[/quote]Thanks ThorpeYea the PHP try/catch system is weak Quote Link to comment https://forums.phpfreaks.com/topic/35738-try-catch/#findComment-169446 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.