Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    Are you able to "break" a try {}?

    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()?

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Perhaps this will help.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

    Edit:
    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:
    PHP Code:
    try
    {
        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.
    Last edited by Fou-Lu; 01-15-2010 at 05:46 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •