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