Execution flow of a Try/catch/Finally block

Asked

Viewed 1,441 times

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...

1 answer

7


How the execution flow works in a Try/catch/Finally block in the example below?

You will enter the Try and play an exception. As predicted, the println after makeException() will not run. After that, program will run block catch(ExceptionBeta and after that the block inside the finally.

The Finally block always runs at the end, regardless of whether an exception has been cast or not (it is particularly useful for "cleaning" at the end of the method)

I also wanted to know, in this same code, how to launch more than one exception, besides Exceptionbeta

In Java, and in all the programming languages I know with exceptions, you can only make one exception at a time.

One possibility is to create a single class for the exception and pass the list of errors as one of the fields in that class.

Is it possible to use logical operators in catch? for example: catch(Exceptionalpha alphaEx && Exceptionbeta betaEx) {...}

I don’t know what you expect && to do in this case. Anyway, I don’t think Java supports something like this.

As for the "or", to treat more than one type of exception in a single catch, in Java 7+ there is the syntax | for that reason:

catch (IOException|SQLException ex) {
    logger.log(ex);
    throw ex;
}

http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

If you are with an older version of Java, the best you can do is capture a superclass of your classes or make several catchs yourself. In the latter case you can try to create a little internal scare to avoid duplicating code.

} catch(ExceptionAlpha alphaEx) { 
    tratarExcecao();
} catch(ExceptionBeta betaEx) {
    tratarExcecao();
}
  • Got it! Thank you very much!

  • What’s the point of using throw ex? Wouldn’t it be much simpler to put throws in the method?

  • In this case it is because I am logging before relaunching the exception.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.