I'm wondering if it's possible to create method for exception handling like this:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
do {
try {
double a = in.nextDouble();
double b = in.nextDouble();
double c = in.nextDouble();
// some code that works with a, b and c variables
}
catch(java.util.InputMismatchException exc) {
System.out.println("Wrong input.");
in.next();
}
} while(true);
}
So I can input some variables from keyboard, if it's not double, then it says "Wrong input." and wants you to type again. Is it possible to create method that will handle all of this? Is it even possible in Java to create method that can work like this:
double a, b, c;
myMethodTest(a);
and would actually work in the same way as the first code? How to do that, if possible? I know that there is an error that says "variable may not have been initialized", but is there a way to work around it? Can method initialize the variable that I put in ()
, like myMethodTest(a)
could possibly initialize a
and do all the exception handling for me?