I just ran into a hidden gem in one of our Java libraries:
for(Widget w : widgets) {
if(shouldDoStuff()) {
try{
// Do stuff to w.
} catch(Exception e){
throw new RuntimeException("Couldn't do stuff.");
} finally{
// Compiler warning: finally block does not complete normally
continue;
}
}
}
I know that finally
trumps everything, but I'm wondering about 2 things:
- What happens when the
catch
clause does execute? Does the exception get thrown or not? What happens first: the thrown exception or thecontinue
statement? - How can I rewrite this to get rid of the warning?
I found this very similar question but the accepted answer just states that the exception will be thrown abruptly, and I'm not sure what that means. Plus it doesn't really help me understand my first question above about the order of events that will transpire.