This week's book giveaways are in the Cloud and AI/ML forums. We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line! See this thread and this one for details.
Why does this program skip the catch block and doesnt throw the second RuntimeException? Instead it goes to the finally block after the first RuntimeException is thrown. Shouldn't the catch block catch the first RuntimeException allowing the prgram to throw a new RuntimeException?
Timothy Giles wrote:. . . . Shouldn't the catch block catch the first RuntimeException allowing the prgram to throw a new RuntimeException? . . .
If you have followed MS' suggestion, you will know that is exactly what happens. But the finally is always executed whether an exception is thrown or not; the exception thrown in the finally takes priority over any exceptions thrown elsewhere.
That is why it is usually a bad idea to throw an exception or return a value from a finally; it overwrites the results of the rest of the method.