/    Sign up×
Community /Pin to ProfileBookmark

Try Catch Won’t Compile

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?

to post a comment
Java

4 Comments(s)

Copy linkTweet thisAlerts:
@chazzyJan 18.2007 — what is the type of kbObj? what is trialDivisors?

Edit: What does the last } in your code match up to?
Copy linkTweet thisAlerts:
@benf101authorJan 19.2007 — Here is everything


// In Class Day:

// Author:

// Java 220

import java.util.*;

public class ExceptionFixed
{

public static void main(String[] args)

{

Scanner kbObj = new Scanner(System.in);

// array of divisors and other useful values
int[] trialDivisors = {3, -3, 12, -12, 0};
int divisorIndex = 0;
int quotient = 0;

boolean validInput = false; //not valid input yet

while(!validInput)
{
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();
}
// catch (Exception e)
// {
// // unanticipated exception
// System.out.println("nUnexpected error: " + e.toString());
// }
}

// have valid input, try the quotient

try
{
// compute quotient using the indicated divisor
quotient = 10 / trialDivisors[divisorIndex];
}
catch(ArithmeticException e)
{
//print error message for div by 0
System.out.println("nAttempt to divide by zero");
System.out.println("Abnormal program termination");
System.exit(1);
}

catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("nAttempt to use invalid array index " + e.getMessage());
System.out.println("Abnormal program termination");
System.exit(2);
}

// catch(Exception e)
// {
// System.out.println("Unexpected error: " + e.toString());
// }
// print results
System.out.println("nResult of dividing 10 by " + trialDivisors[divisorIndex] + " is " + quotient);
System.out.println("Normal Termination");
System.exit(0);
}

}
Copy linkTweet thisAlerts:
@chazzyJan 19.2007 — it compiles fine for me, even when i uncomment your exceptions. what version of java are you using? i'm using 1.5
Copy linkTweet thisAlerts:
@benf101authorJan 20.2007 — I guess we're not using the right version then. I think it could be an earlier version.

Thanks for doing that. You're a big help!
×

Success!

Help @benf101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...