Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 3.61 KB

153d.md

File metadata and controls

35 lines (25 loc) · 3.61 KB

Back to questions

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 the switch statement, and throw an exception of type A. This is not matched by the catch block for exceptions of type C, or the catch block for exceptions of type B, but it is matched by the catch block for exceptions of type A. In this catch block, the exception e is printed, so exception A is printed to the console (this comes from the toString method in class A). The exception is then thrown again. It does not match the catch block for exceptions of type NumberFormatException, but it does match the catch block for exceptions of type Exception, thus we see the text: An exception was thrown: exception A. After this, the finally 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 the switch statement, and throw an exception of type B. This is not matched by the catch block for exceptions of type C, but it is matched by the catch block for exceptions of type B. In this catch block, the exception e is printed, so exception B is an exception A is printed to the console (this comes from the toString method in class B). The exception is then thrown again, and is matched by the catch block for exceptions of type A (because B is a subclass of A). In this catch block, the exception e is printed, so exception B is an exception A is printed to the console again. The exception is then thrown once again. It does not match the catch block for exceptions of type NumberFormatException, but it does match the catch block for exceptions of type Exception, thus we see the text: An exception was thrown: exception B is an exception A. After this, the finally 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 the switch statement, and throw an exception of type C. 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 any catch block, and the text No exception was thrown. is output. The finally block is still executed, leading to the output All control-flow paths get to me!.

  • command-line argument "1.2". With this argument, parseInt throws a NumberFormatException, which is caught by the catch block for exceptions of type NumberFormatException. Thus the message The command-line argument you entered was not an integer! is output. This is followed by execution of the finally block as usual.

  • no command-line arguments. With no command-line arguments, an ArrayIndexOutOfBoundsException is thrown when the argument args[0] to parseInt is evaluated. This exception is caught by the catch block for exceptions of type Exception, and we see the text: An exception was thrown: java.lang.ArrayIndexOutOfBoundsException: 0. After this, the finally block is executed as usual.