Background
Exceptions are used in Java among other programming languages so programs don’t crash during critical moments, which could ruin user experience or cause outages (think.. what if people couldn’t like your pictures on Instagram because of an IndexOutOfBoundsException?). When code is executed that might crash the program, programmers can use a try/catch block to protect from unnecessary program crashes. The catch portion of the block shows which code to execute in the case that a specific Exception is caught.
Exercise
Your job is to modifyLab12.javaso that it is a working program and that does not crash when it receives bad user input. Lab12.java, as it stands today, has an empty main() method. Before submitting Lab12, your main method must do the following:
Get an int value from the user
Call the static fib() method with the user’s int as the sole argument.
If the user enters an int whose fibonacci number can not be calculated, java will throw a specific error when you call fib().
It is your job to find out what types of ints don’t work with fibonacci calculation and what this error is!!!
Wrap the code that calls fib() in a try/catch block to catch the specific error you saw above, and print out a message saying the input is bad, and the user should try again.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here