View Full Version : Are you able to "break" a try {}?

01-15-2010, 04:09 AM
Just want to point it out here: I don't know a lot about exceptions.

For a try{} statement, if I were to throw an exception will it automatically jump to the catch() OR will it continue executing the script? Or is there a command to break the try{} and go straight/away from the catch()?

01-15-2010, 04:13 AM
Perhaps this (http://us2.php.net/exceptions) will help.

01-15-2010, 05:43 AM
It will always jump to the catch if an exception can be caught and is thrown. If it cannot be caught, it will trigger a fatal error (not to be mistaken as an exception). Once caught, it will continue with the remaining steps of the process unless you've rethrown or generated a new exception. You can catch more than one exception if more than one potential exception can be thrown and react accordingly. PHP does not support a finally clause.

Best I know, you cannot actually initiate a break within a catch since this doesn't make sense. You can die if you desire to though.

BTW, it sounds like you're asking how to avoid the catch. You don't, if you don't execute the catch, it will die unless you're controlling you're error handling which doesn't sound all that pleasant. You're catch is not required to do anything though:

throw new Exception('Tossing an exception');
catch (Exception $ex)
// All done, we don't care about Exception
// more stuffs.

With this in mind, you can create you're own exceptions that extend an exception base (runtime, base exceptions, etc), and if any of those are caught, just do nothing with them.