I am a beginner with Java. I need to know when Catching Exceptions is
there some way I can have the Catch go back to the line of code that
caused the Exception, or would I have to use a While statement for
Java to jump back to the line of code? For example in the code below
I have three 3 variable and would like to make it so when the
Exception occurs it will go back to the line of code that caused it.
In the code I use 1 While and 1 Catch statement for 3 variables, and
when there is an Exception in variable #3 then it will make the user
go back to and reinput variable 1, 2, and 3. I know I could get
around this by writing more While and Catch statements, but when I
have many variables, I don't want to have to put in a While and Catch
statement for each variable.
import javax.swing.JOptionPane;
class MyClass{
int int1 = 0;
int int2 = 0;
int int3 = 0;
String y = "y";
public void MyInt(){
while(y.equals("y")){
try{
int1 = Integer.parseInt(JOptionPane.showInputDialog("type the 1
number"));
int2 = Integer.parseInt(JOptionPane.showInputDialog("type the 2
number"));
int3 = Integer.parseInt(JOptionPane.showInputDialog("type the 3
number"));
y = "";}
catch(java.lang.NumberFormatException x){
JOptionPane.showMessageDialog(null, "you have to make an entry");
y = "y";}
}//ends while
JOptionPane.showMessageDialog(null, "int1 " + int1 + "\nint2 " + int2
+ "\nint3 " + int3);}//ends MyInt
}//ends MyClass