PositiveGuy
PositiveGuy

Reputation: 47743

Exit Try/Catch to prevent code after from being run

I've got for example a try/catch in my method:

    }
    catch (OurCustomExceptionObject1 ex)
    {
       txtErrorMessage.InnerHtml = "test 1";
    }
    catch(OurCustomExceptionObject2 ex)
    {
        txtErrorMessage.InnerHtml = "test 2";
    }
    catch (OurCustomExceptionObject3 ex)
    {
        txtErrorMessage.InnerHtml = "test 3";
    }

    ... rest of code here is being executed after the try/catch

I do not want the rest of code to run if any of the exceptions are caught. I'm handling the exceptions. I heard do not use Exit Try for some reason. Is that true, it's bad to do this? Is this the right way to halt execution of code thereafter the catch statement?

Upvotes: 19

Views: 85215

Answers (5)

Alec C
Alec C

Reputation: 11

Basically, like ilMes above. Just more complete.

   do
    {
        try
        {    
         //do try something ...
        }
        catch (OurCustomExceptionObject1 ex)
        {
           txtErrorMessage.InnerHtml = "test 1";
           break;
        }
        catch(OurCustomExceptionObject2 ex)
        {
            txtErrorMessage.InnerHtml = "test 2";
            break;
        }
        catch (OurCustomExceptionObject3 ex)
        {
            txtErrorMessage.InnerHtml = "test 3";
            break;
        }
        
        UnwantedFunctionIfCatchIsTriggered();//this code won't run if break is called
    }while(false)
     
    //wanted code continues.

Upvotes: 1

ilMes
ilMes

Reputation: 11

How to do a VB Exit Try:

try {
   for(;;) {
      if( x>y ) break;  // forced exit try

      ....

      break; // natural exit try, must be present
   }
}
catch {
...
} 
finally {
...
}

Upvotes: 1

DaveDev
DaveDev

Reputation: 42185

From a high level view, I think this could be a violation of (at least) the Single Responsibility Principle if your code is trying to do something that could fail, and then go on to do some more stuff.

For the sake of an answer though, if you wanted to do a hack (which is always bad, so don't) you could do

bool success = true;
try
{
    // the good ol' college try
}
catch (...)
{
    success = false;
}

if (success)
{
    // do the rest of your stuff
}

edit: or alternatively as BlueRaja suggested, put all of your code into the try block. If the first bit fails, it fails. The rest of the code won't run anyway.

Upvotes: 1

Dan Tao
Dan Tao

Reputation: 128307

Two options immediately come to mind:

  1. return straight from inside each catch (as BlueRaja suggested)
  2. set a flag (e.g., errorOccurred) within the catch blocks for the exceptions you don't want to allow, then put if (errorOccurred) return; after the whole try/catch block

The latter might be more readable to other developers, since it's easy to skim past what happens inside a catch to figure out what happens afterward. Seeing a blatant if (errorOccurred) return; makes it pretty hard to misunderstand what happened.

Upvotes: 2

Either return in the catch-block, rethrow the exception, or move the code from below the try-block inside the try-block.

Upvotes: 40

Related Questions