my dog learned polymorphism*
The moose likes Beginning Java and the fly likes exception handling help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception handling help" Watch "exception handling help" New topic
Author

exception handling help

Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
I am working on a project for my java class that has to do with exception handling. What i am trying to do is use a try catch block with multiple catches. when i run my program it just puts out an infinite loop. What it has to do is catch the InputMismatchException and then loop back to have the user retype two integers. I also have to define my own exception for dividing by zero...but that�s a question for later...please help...code below...thanks
----------------------------------------------------------------------------


EDIT by mw: Added Code Tags.
[ February 07, 2007: Message edited by: marc weber ]
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
wow sorry about that...i had everything indented and everything...guess this forum didnt like it
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Jason Mackie:
...when i run my program it just puts out an infinite loop. What it has to do is catch the InputMismatchException and then loop back to have the user retype two integers...

Welcome to JavaRanch!

When the InputMismatchException is thrown, the Scanner does not advance, so it's still trying to scan whatever input caused the exception. To advance the Scanner past the "bad" input, try adding the following as the first line of your catch block...

scan.nextLine();


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jason Mackie
Greenhorn

Joined: Feb 07, 2007
Posts: 17
awesome thanks marc that worked...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception handling help
 
Similar Threads
Calling Methods Help
fyi: pre/post increment with exception thrown on the line
Fibonacci
help with joption pane cancel button
OK back with another Exception Handeling question