Types of Exception

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:

Yes. (Because the string "34.56" can't be converted into an int).

Types of Exception

Here are two of the rules about how try/catch blocks work:

  1. The first catch{} block to match the type of Exception thrown gets control.
  2. The most specific Exception types should appear first in the structure, followed by the more general Exception types.

To make full sense of these rules you need to know more about Exception types. Here is a hierarchy diagram of Exception:

An ArithmeticException is thrown for some types of arithmetic problems, such as when a division by zero is attempted. (However, not all arithmetic problems cause an ArithmeticException.)

In arranging the try{} blocks, a child class should appear before any of its ancestors. If class A is not an ancestor or descendant of class B, then it doesn't matter which appears first.

QUESTION 10:

Should ArithmeticException appear before RunTimeException in the list of catch statements?