What happens to the return A code in my catch block?
public class TryCatchFinallyTest {
@Test
public void test_FinallyInvocation()
{
String returnString = this.returnString();
assertEquals("B", returnString);
}
String returnString()
{
try
{
throw new RuntimeException("");
}
catch (RuntimeException bogus)
{
System.out.println("A");
return "A";
}
finally
{
System.out.println("B");
return "B";
}
}
}
The finally get’s executed right before any return’s / exits from the method. Therefore, when you do
it executes like so:
And thus the “B” is returned, not the “A”