6
How the execution flow works in a block try/catch/finally
in the example below?
public class ExceptionTest {
public static void makeException() throws ExceptionAlpha, ExceptionBeta, ExceptionGamma {
throw new ExceptionBeta();
}
public static void main(String [] args) {
try {
makeException();
System.out.println("Isto não vai ser executado.");
} catch(ExceptionAlpha alphaEx) {
System.out.println("Exceção Alpha foi lançada.");
} catch(ExceptionBeta betaEx) {
System.out.println("Exceção Beta foi lançada.");
} catch(ExceptionGamma gammaEx) {
System.out.println("Exceção Gamma foi lançada.");
} finally {
System.out.println("Isto será executado, independente se houver exceções.");
}
}
}
Is it possible to use logical operators in catch? for example: catch(ExceptionAlpha alphaEx && ExceptionBeta betaEx) {...}
I also wanted to know, in this same code, how to launch more than one exception, besides the ExceptionBeta
and how the code execution flow.
Thank you!
It’s more of a personal opinion but I prefer to use exceptions just to "rattle off" and abort the entire program. When you start capturing exceptions, in particular capturing exceptions in complicated patterns it is quite difficult to predict what is the execution flow that your program will actually take and ensure that you have not left any object with an invalid internal state...
– hugomg