0

I want to execute a block of code even if ANY exception is caught in any of the preceding Try/Catch blocks, rather than having Finally blocks after every single Try/Catch. Is this possible to do?

Example:

public void Method()
{
  try
  {
  }
  catch ( Exception e )
  {
  }
  try
  {
  }
  catch ( Exception e )
  {
  }
  try
  {
  }
  catch ( Exception e )
  {
  }

  finally
  {
    // Execute no matter what 
  }
}

2 Answers2

0

You just need to wrap all your try/catch blocks inside another try/catch block, with a finally block at the end.

public void Method()
{
  try
  {
    try
    {
    }
    catch ( Exception e )
    {
    }
    try
    {
    }
    catch ( Exception e )
    {
    }
    try
    {
    }
    catch ( Exception e )
    {
    }
  }
  catch ( Exception e )
  {
  }            
  finally
  {
    // Execute no matter what 
  }
}
Caleb George
  • 234
  • 1
  • 10
  • 2
    You can just `try{ }finally{}` with no `catch` BTW. – spender Aug 27 '21 at 16:16
  • @spencer I didn't see this and thought the same at first glance after code formatted, but here it's fine and clean because no matter what is done in each capture and all exceptions will go through the final. I sometimes use this kind of thing, especially when initializing forms, to not break everything and get the most before showing the problems, or not, without crashing the app. –  Aug 27 '21 at 16:26
  • @OlivierRogier er.. what? There will never be an exception to catch in the outer try, so there's no point putting a handler for it? – Caius Jard Aug 27 '21 at 16:30
  • 1
    @CaiusJard Indeed, here there is no code, pure theory. But imagine in real code, you log, but the logger crashes for some reason, that should not almost not occur, thus you can get it in the outer catch, right? –  Aug 27 '21 at 16:31
  • Yes yes, I'm sure we're all capable of imagining a hundred scenarios not present in the original question - but I'm never quite sure why SO denizens do it. We're software engineers, we work to a spec, we don't invent parts of spec that aren't there and then invent more things to deal with the nonexistent things.. (Except when we want to generate some debate about something that we aren't even sure exists.. And that's the part I don't understand why people engage in) – Caius Jard Aug 27 '21 at 16:35
0

As an alternative to the proposal on the other answer (which I don't disagree with) consider avoiding excessive nesting; there is nothing stopping you try/finally'ing the method call instead

try{
  Method();
} finally {

}

If the method needs something that you're planning to set up/dispose of in the try/finally, you can pass it to the method..

You could, in your case, also put the finally on the last try/catch so long as your other catch blocks won't throw an exception themselves

Main learning point is: You can put a try inside a try. You don't have to put a catch; you can just have a finally. If an exception occurs part way through the try of a try..finally, the try code stops and the finally code is run, then the exception that occurred in the try propagates after the finally is done. If an exception occurs in a finally it replaces any exception that occurred in a try. If there was no exception in a try, the code in the finally runs after the last line of the try

Caius Jard
  • 72,509
  • 5
  • 49
  • 80