Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Catching Exceptions

  Asked By: Ira    Date: Oct 16    Category: Java    Views: 597
  

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


Share: 

 

4 Answers Found

 
Answer #1    Answered By: Eddie Austin     Answered On: Oct 16

The best way I know to solve that is (modifications are marked with
a '*':

public  void MyInt(){
*boolean continue = true; //Control variable
*while(continue){ //evaluate
> 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 = "";}
* continue = false; //if no problem was detected
> catch(java.lang.NumberFormatException x){
* continue = true; //if a problem was detected, making possible
* another cicle
> JOptionPane.showMessageDialog(null, "you have to make an
entry");}
> }

 
Answer #2    Answered By: Antonio Dunn     Answered On: Oct 16

I still can't isolate the Exception to the line  of code  that caused
it. I dont want to put  in 1000's of While/Catch statements  when I
have many variables. Is there some way to get the Catch back  to the
line that caused without 1000's of While/Catch statements?

 
Answer #3    Answered By: Holly Brown     Answered On: Oct 16

Sometimes your code  may warrant lots of try/catch blocks. But when
that does happen, you should re-examine your design to see if perhaps
you are coding procedurally instead of objectly (is that a word?)

In this case, you might try the exception's toString() method to see
if it will give you enough detail to tease out the line  number. That
will work for a school project.

In this case, you might set up a loop:
While(...) {
try {
for (int i=1;i<4;i++) {
intV = Integer.parseInt(JOptionPane.showInputDialog("type the " + i
+ " number"));}
catch  (Exception e) { examine value of i to determine which one
failed}
} // end while

a19859 suggests using a boolean variable. You could add that advice
into this advice and have strong control over the inputs.

There are several other ways to handle this issue, but the general
idea is to merge your design with suggestions to arrive at a solution.

 
Answer #4    Answered By: Maliha Malik     Answered On: Oct 16

The best way to handle this is not to use a try/catch block, but to
get the text from the JoptionPane and determine if it is null BEFORE
you parse the int... Otherwise you will have to put  a try/catch on
each call.

You could also create an array of int[] and loop through the

for(int i=0; i<numberOfIntegers; i++){
try{
int[i] = Integer.parseInt(JOptionPane.showInputDialog("type the
number"));
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "you have to make an entry");}
i = i-1;
}
}

You could have an array of Strings which would give you a different
message for each loop... etc etc...

That way, you don't have to have 1000's of try/catches, and you
ALWAYS know which input failed.

 
Didn't find what you were looking for? Find more on Catching Exceptions Or get search suggestion and latest updates.




Tagged: