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...

1 answer below »

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 modify
Lab12.java
so 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.






Answered Same DayApr 19, 2021

Answer To: Background Exceptions are used in Java among other programming languages so programs don’t crash...

Neha answered on Apr 21 2021
149 Votes
import java.util.Scanner;
public class lab12
{
public static void main(String[] args)
{

Scanner myObj = new Scanner(System.in);
try
{
System.out.println("Enter enter an int:");
int number = myObj.nextInt();
if...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here