Solution to 153d: Exceptions and inheritance (iii)
See code at solutions/code/tutorialquestions/question153d
-
command-line argument "0".
parseIntgives us the integer 0, so we hit case 0 of theswitchstatement, and throw an exception of typeA. This is not matched by thecatchblock for exceptions of typeC, or thecatchblock for exceptions of typeB, but it is matched by thecatchblock for exceptions of typeA. In thiscatchblock, the exceptioneis printed, soexception Ais printed to the console (this comes from thetoStringmethod in classA). The exception is then thrown again. It does not match thecatchblock for exceptions of typeNumberFormatException, but it does match thecatchblock for exceptions of typeException, thus we see the text:An exception was thrown: exception A. After this, thefinallyblock is executed, so we finally see the text:All control-flow paths get to me!. -
command-line argument "1".
parseIntgives us the integer 1, so we hit case 1 of theswitchstatement, and throw an exception of typeB. This is not matched by thecatchblock for exceptions of typeC, but it is matched by thecatchblock for exceptions of typeB. In thiscatchblock, the exceptioneis printed, soexception B is an exception Ais printed to the console (this comes from thetoStringmethod in classB). The exception is then thrown again, and is matched by thecatchblock for exceptions of typeA(becauseBis a subclass ofA). In thiscatchblock, the exceptioneis printed, soexception B is an exception Ais printed to the console again. The exception is then thrown once again. It does not match thecatchblock for exceptions of typeNumberFormatException, but it does match thecatchblock for exceptions of typeException, thus we see the text:An exception was thrown: exception B is an exception A. After this, thefinallyblock is executed, so we finally see the text:All control-flow paths get to me!. -
command-line argument "2".
parseIntgives us the integer 2, so we hit case 2 of theswitchstatement, 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
parseIntgives us the integer 3, which does not match any of the cases in the switch. Thus we do not enter anycatchblock, and the textNo exception was thrown.is output. Thefinallyblock is still executed, leading to the outputAll control-flow paths get to me!. -
command-line argument "1.2". With this argument,
parseIntthrows aNumberFormatException, which is caught by thecatchblock for exceptions of typeNumberFormatException. Thus the messageThe command-line argument you entered was not an integer!is output. This is followed by execution of thefinallyblock as usual. -
no command-line arguments. With no command-line arguments, an
ArrayIndexOutOfBoundsExceptionis thrown when the argumentargs[0]toparseIntis evaluated. This exception is caught by thecatchblock for exceptions of typeException, and we see the text:An exception was thrown: java.lang.ArrayIndexOutOfBoundsException: 0. After this, thefinallyblock is executed as usual.