0

I would like to check user input value against a declared final variable to make sure it does not exceed the final int variable for the purpose of using in a try-catch-finally.

final int variable = 20; // Variable declared for max allowable
int userInput = 0; // Used to hold user input from GUI interface
try {
userInput > variable; // This does not work
}
catch (ArithmeticException e) {
System.out.println("Your input exceeds max allowble.");
}
finally {
System.out.println("Good, input does not exceed max allowable.");
}
}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by jems5
0

You have to use an if test in some form or another.
If you really want to use catch/finally to handle it you can throw a new Exception, as in

if (somethingIsWrong) throw new Exception("Something went wrong");

(although some writers think you shouldn't use exceptions for ordinary user errors)

0

You have to use an if test in some form or another.
If you really want to use catch/finally to handle it you can throw a new Exception, as in

if (somethingIsWrong) throw new Exception("Something went wrong");

(although some writers think you shouldn't use exceptions for ordinary user errors)

Great thanks - I will close now.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.