tags:

views:

419

answers:

5

how can i rewrite the following code using try and catch statements,catching any exceptions that might be thrown

for (j=0; j<limit; j++)
{
   x=Integer.parseInt(keyboard.readLine());
   y[j]=1/x;
}
+27  A: 

That depends if you want the loop to end if there is an error, or you want it to continue...

# loop continues on errors
for ..
  try
    dostuff
  catch something
    fix

vs

# loop ends on errors
try
  for ..
    dostuff
catch something
  ..
Tor Valamo
+1 for not writing the code for him, but giving him the right answer.
Brandon
@Brandon, agreed. +1
Rob Wells
In the former case you can elect to discard the offending input and continue.In the later case the y[] array will likely contain a partial set of data in the event of an exception. You may need to clean that up as part of the fix.
Chris Nava
A: 

Java like pseudocode

for (j=0; j<limit; j++)
{
 try{
   x=Integer.parseInt(keyboard.readLine());
   y[j]=1/x;
 }
 catch(some_exception_not_gonna_say_which_one sengswo){
    //handle exception
 }
}
Tom
+1  A: 

I am guessing that you want a certain number of integers from the user. A while loop will probably make more sense here than a for loop as you will want to loop until you have the correct number of inputs form the user.

Also, in most cases y[j] is going to hold a zero, as 1 / x is 0 for x > 1. use 1.0/x instead to get floating point math and not integer math.

while j < limit
   try
      // parse number
      // store number
      // increment j
   catch
      // inform user of error
end while
Brandon Bodnár
+1  A: 

Here is how you can do it in Java. It's better to surround the loop with the try/catch instead of placing the try/catch inside the loop when possible for performance reasons. However, if you want the loop to continue when an error occurs, then you will need to place the try/catch inside the loop.

Loop inside try/catch:

try {
    for (j=0; j<limit; j++)
    {
       x=Integer.parseInt(keyboard.readLine());
       y[j]=1/x;
    }
} catch(Exception e) {
    e.printStackTrace();
}

Loop outside try/catch:

for (j=0; j<limit; j++) {
    try {
        x=Integer.parseInt(keyboard.readLine());
        y[j]=1/x;
    } catch(Exception e) {
        e.printStackTrace();
    }
}
hoffmandirt
Given the obvious homework nature -- and the serial homework nature of this user -- it's hard to support giving him/her actual answers.
delfuego
@Delfuego - we're not teachers. I don't know *why* he is asking, and assuming, he's just lazy, is not fair. There may be other reasons. To me, this is a helpful answer according to SO FAQ, so +1.
Andreas_D
Of course we are not teachers but if someone asking such a question, he should at least provide information what he has done/understood so far and not just asking in a way "please, do this for me"...
Felix Kling
Learning is also a goal of SO (I suppose) and this way he does not learn anything.
Felix Kling
@AndreasD - assuming he's lazy after one such question would, as you say, be unfair. But assuming he's lazy after five in the last two hours, none of which he has provided (a) any of his own work, (b) any clarification, or (c) any followup? I think that's perfectly fair.
delfuego
But to me that's no excuse or reason to downvote useful answers. It may not be useful from a teachers perspective but useful for asong84, maybe even more useful than Tor's answer. Please don't downvote correct and comprehensive answers juts because you think, the guy who asked doesn't deserve it. That's neither fair nor encouraging to people that take their time to answer questions.
Andreas_D
Whether or not it's homework it's still a good question to ask and whether or not he takes the answer and learns from it is his choice. If I can help somebody else in the future with the answer to this question, then I'll take the down votes. And even though it's obvious that this is homework based on his other questions, we are still only making assumptions that it is.
hoffmandirt
I am a teacher and I would not give the actual answer - just a pointer/hints
TofuBeer
+1  A: 

The real question is what do you want to do with the exception (i.e. why even bother catching it)? I would think that you would want to verify or cleanse whatever keyboard input you're getting (IMO a programmer should never trust a user's input), so you would want to ensure that the input is a digit (hint: regex).

There are basically three questions that you need to ask in this scenario: can I recover from this exception, should an exception halt execution, or should I ignore the exception (eat it, and continue to the next number)?

Droo