Exception to throw for missing input from scanner | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Exception to throw for missing input from scanner

I wanted to make a program not crash when it doesn't get the input (String) it needs... while(true){ try{ str = scan.nextLine(); } catch(/*The exception I don't know and didn't find*/ e) { System.out.println("Missing Input. Please try again"); continue; } break; }

28th Jan 2017, 10:20 AM
Kai Kiehl
Kai Kiehl - avatar
3 Answers
+ 3
Just use "catch(Exception e)". The word "Exception" is made to handle all possible exceptions that might occur.
28th Jan 2017, 12:03 PM
Nikolay Ganev
Nikolay Ganev - avatar
+ 2
you can also check if there is a next line, scanner.hasNextLine(). Returns a boolean if it has a next line
28th Jan 2017, 1:36 PM
Daniel S
Daniel S - avatar
0
Thank you both!
28th Jan 2017, 1:43 PM
Kai Kiehl
Kai Kiehl - avatar