Omitting the catch{} Blocks

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

In which examples did the try{} block execute without an exception? 1
In which examples did the try{} block throw an ArithmeticException? 2
In which examples did the try{} block throw an unhandled exception (one with no catch{} block)? 3
In which examples did the finally{} block execute? 1, 2, 3

Omitting the catch{} Blocks

If there is a finally{} block, then the catch{} blocks can be omitted:

try
{
  // statements, some of which might
  // throw an exception
}

finally
{
  // statements which will execute no matter
  // how the try block was exited.
}

// Statements following the structure

This might be done if you don't want to handle Exceptions in your method, but you have a few statements that must execute no matter what before control is returned to the caller. This often happens if a method has a lock on some resource that it should give up before exiting. For example, if the method opens a file, it should (perhaps) close it before exiting.

QUESTION 19:

Why can't the "Statements following the structure" be used to close a file that the method has opened?