I am doing a project in class. We have to make a program that scans Two Postive Integers, one at a time, using try-catch block. Can some one please help me get started?
Kayla1993
0
Newbie Poster
Recommended Answers
Jump to PostYou have to first create a class that
inherits
the Exception class because that is how a try-catch block is implemented.Throw an error if the input number is less than zero.
Hope you have been taught exception handling.But if it weren't for an assignment,seriously,I do not get the point …
Jump to Postit still doesn't catch, because you still don't throw an exception. the catch will only be executed, if there is an exception to be caught.
Jump to Post.Throw an error if the input number is less than zero.
You missed this part out. The catch is OK, but you don't throw the exception when the input is wrong.
Jump to PostI dont understand, How am I not throwing an exception?
eg
if (! ans.equals("y") || ans.equals("n")) throw new InputMismatchException("must be y or n");
All 13 Replies
delta_frost
0
Newbie Poster
stultuske
1,116
Posting Maven
Featured Poster
Kayla1993
0
Newbie Poster
stultuske
1,116
Posting Maven
Featured Poster
Kayla1993
0
Newbie Poster
stultuske
1,116
Posting Maven
Featured Poster
Kayla1993
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Kayla1993
0
Newbie Poster
Kayla1993
0
Newbie Poster
Kayla1993
0
Newbie Poster
mlesniak
22
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.