eldan88 Posted September 26, 2013 Share Posted September 26, 2013 Hey guys. I am learning how to work with the try/throw exception. I wrote a small sample code, but for some reason its not being caught. Not sure what I am doing wrong. Can you guys please help? Below is a code. (Its a little bit of a goof) $age = 20; try { if($age > 21) { echo "You are legal to drink"; } else { throw new Exception("You are not legal drink"); } } catch (Exception $e) { echo $e->getMessage(); } Quote Link to comment https://forums.phpfreaks.com/topic/282462-throw-excpetion-not-being-caught/ Share on other sites More sharing options...
requinix Posted September 26, 2013 Share Posted September 26, 2013 Works for me. Why do you say it isn't being caught? Quote Link to comment https://forums.phpfreaks.com/topic/282462-throw-excpetion-not-being-caught/#findComment-1451330 Share on other sites More sharing options...
AbraCadaver Posted September 26, 2013 Share Posted September 26, 2013 Works great for me: You are not legal drink What were you expecting? Quote Link to comment https://forums.phpfreaks.com/topic/282462-throw-excpetion-not-being-caught/#findComment-1451331 Share on other sites More sharing options...
vinny42 Posted September 27, 2013 Share Posted September 27, 2013 but for some reason its not being caught. Does that mean no exception is thrown, or do you get "uncaught exception"? The latter is something you can get if your script is in a namespace, because you are catche "Exception $e" and not "\Exception $e" Quote Link to comment https://forums.phpfreaks.com/topic/282462-throw-excpetion-not-being-caught/#findComment-1451376 Share on other sites More sharing options...
Solution eldan88 Posted September 27, 2013 Author Solution Share Posted September 27, 2013 Hey Never mind. You guys where right. I saved the page again, refreshed it and it worked. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/282462-throw-excpetion-not-being-caught/#findComment-1451409 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.