Solution to 153d: Exceptions and inheritance (iii)
See code at solutions/code/tutorialquestions/question153d
-
command-line argument "0".
parseInt
gives us the integer 0, so we hit case 0 of theswitch
statement, and throw an exception of typeA
. This is not matched by thecatch
block for exceptions of typeC
, or thecatch
block for exceptions of typeB
, but it is matched by thecatch
block for exceptions of typeA
. In thiscatch
block, the exceptione
is printed, soexception A
is printed to the console (this comes from thetoString
method in classA
). The exception is then thrown again. It does not match thecatch
block for exceptions of typeNumberFormatException
, but it does match thecatch
block for exceptions of typeException
, thus we see the text:An exception was thrown: exception A
. After this, thefinally
block is executed, so we finally see the text:All control-flow paths get to me!
. -
command-line argument "1".
parseInt
gives us the integer 1, so we hit case 1 of theswitch
statement, and throw an exception of typeB
. This is not matched by thecatch
block for exceptions of typeC
, but it is matched by thecatch
block for exceptions of typeB
. In thiscatch
block, the exceptione
is printed, soexception B is an exception A
is printed to the console (this comes from thetoString
method in classB
). The exception is then thrown again, and is matched by thecatch
block for exceptions of typeA
(becauseB
is a subclass ofA
). In thiscatch
block, the exceptione
is printed, soexception B is an exception A
is printed to the console again. The exception is then thrown once again. It does not match thecatch
block for exceptions of typeNumberFormatException
, but it does match thecatch
block for exceptions of typeException
, thus we see the text:An exception was thrown: exception B is an exception A
. After this, thefinally
block is executed, so we finally see the text:All control-flow paths get to me!
. -
command-line argument "2".
parseInt
gives us the integer 2, so we hit case 2 of theswitch
statement, and throw an exception of typeC
. Following an argument analogous to the above, we then see the following output:
exception C is an exception B is an exception A
exception C is an exception B is an exception A
exception C is an exception B is an exception A
An exception was thrown: exception C is an exception B is an exception A
All control-flow paths get to me!
-
command-line argument "3". This time
parseInt
gives us the integer 3, which does not match any of the cases in the switch. Thus we do not enter anycatch
block, and the textNo exception was thrown.
is output. Thefinally
block is still executed, leading to the outputAll control-flow paths get to me!
. -
command-line argument "1.2". With this argument,
parseInt
throws aNumberFormatException
, which is caught by thecatch
block for exceptions of typeNumberFormatException
. Thus the messageThe command-line argument you entered was not an integer!
is output. This is followed by execution of thefinally
block as usual. -
no command-line arguments. With no command-line arguments, an
ArrayIndexOutOfBoundsException
is thrown when the argumentargs[0]
toparseInt
is evaluated. This exception is caught by thecatch
block for exceptions of typeException
, and we see the text:An exception was thrown: java.lang.ArrayIndexOutOfBoundsException: 0
. After this, thefinally
block is executed as usual.