I’m a Java student. I’m trying to get a “try catch” to work but it will not compile. Here’s the code:
try
{
// get an element to use
System.out.println(“enter index of divisor [0..” + (trialDivisors.length -1) + “]: “);
divisorIndex = kbObj.nextInt();
// if statement is ok, we want to end loop
validInput = true; // valid input
}
catch(InputMismatchException e)
{
System.out.println(“Invalid input. Please give an integer”);
String junk = kbObj.nextLine();
}
/* HERE IS THE PART THAT WILL NOT COMPILE
catch (Exception e)
{
// unanticipated exception
System.out.println(“nUnexpected error: ” + e.toString());
}
}
I get an compiling error that says:
found : Exception required: java.lang.Throwable
The prof didn’t know the answer. The slideshow for the class said to do it exactly like we did. All of my google searches seemed to indicate that we are doing it correctly, but the compiler didn’t like it.
Any thoughts?