try/catch flow control

아무거나

Rest of the try block does not run if it fails and goes straight to the catch block.

finally

A finally block is where you put code that must run regardless of an exception.

try {
turnOvenOn();
x.bake();
} catch (BakingException e) {
e.printStackTrace();
} finally {
turnOvenOff();
}

Without finally, you have to put the turnOvenOff() in both the try and the catch because you have to turn off the oven no matter what. A finally block lets you put all your important cleanup code in one place instead of duplicating it like this:

try {
turnOvenOn();
x.bake();
turnOvenOff();
} catch (BakingException e) {
e.printStackTrace();
turnOvenOff();
}

If the try block fails (an exception), flow control immediately moves to the catch block. When the catch block completes, the finally block runs. When the finally block completes, the rest of the method continues on.

If the try block succeeds (no exception), flow control skips over the catch block and moves to the finally block. When the finally block completes, the rest of the method continues on.

If the try or catch block has a return statement, finally will still run! That is surprising to me. Flow jumps to the finally, then back to the return.

Exception rules

1) cannot have a catch/finally without a try 2) cannot put code between try and catch 3) try MUST be followed by either a catch or finally 4) try with only a finally(no catch) must still declare the exception, which is kind of pointless bcos try without a catch does not satisfy handle or delcare law

void go throws FooException{
    try{
        x.doStuff();
  } finally{}
  }