main() calls MethodA MethodA calls parseInt() parseInt() throws a NumberFormatException. MethodA catches the exception and prints "Bad Input Data!!" The return statement executes. Control returns to main().
Here is the output of the program when the user enters "0". The stack trace is a "snapshop" of the situation at the time of the exception:
Enter the divisor: 0 Division by zero!! java.lang.ArithmeticException: / by zero at BuckPasser.methodB(BuckPasser.java:9) at BuckPasser.methodA(BuckPasser.java:32) at BuckPasser.main(BuckPasser.java:51)
Here is the output of the program when the user enters "Rats".
Enter the divisor: rats Bad Input Data!! java.lang.NumberFormatException: rats at java.lang.Integer.parseInt(Integer.java:409) at java.lang.Integer.parseInt(Integer.java:458) at BuckPasser.methodA(BuckPasser.java:21) at BuckPasser.main(BuckPasser.java:51)
If you have saved the program to a file, change it
so that main()
is where both types of exceptions
are caught and handled.
Then change it so that each method catches its own exceptions.
There are several other variations you may think of and try.