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(); } 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? 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? 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" Link to comment https://forums.phpfreaks.com/topic/282462-throw-excpetion-not-being-caught/#findComment-1451376 Share on other sites More sharing options...
eldan88 Posted September 27, 2013 Author Share Posted September 27, 2013 Hey Never mind. You guys where right. I saved the page again, refreshed it and it worked. Thanks! 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
Archived
This topic is now archived and is closed to further replies.