• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Handling  RSS feed

 
pk kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have this scenario,

try{

line 1....
line 2....
line 3....
line 4....
line 5....
line 6....
line 7....
line 8....

} catch(Exception e){
Handle Exception;
}

Now if an exception occurs in line 2, the control moves to the catch block. But after i handle the exception is there any way of returning the control back to line 3.

I want to avoid putting try catch around each line and execute each line irrespective of the result of the execution of prev line.

Regards
PK
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...

If line 3 is something you need to do no matter what you could put it in a finally block. If line 2 is the only thing that will throw an exception and you can handle it and you want to go onto line 3 after that then you should move line 3 out of the try block or into its own try block.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now, if you need to run each and every line, no matter what, and each and every line can throw an exception, then i think you're kind of stuck.

i think you will have to wrap each line in it's own try/catch block. I'm sure someone will come along soon and correct me if i'm wrong.
 
pk kumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually thats exactly the scenario. I need to catch each line, and also execute each line irrespective of the result from the prev line.

All the lines will be throwing the same type of exception.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How similar are these lines? Can you move them into a method that swallows exceptions and call it 7 times?
 
Benjamin Lau
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this work?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posted by Benjamin Lau
this work?
No.

The "goto" keyword never works. You could try "break label;" but actually in the snippet you posted, "break;" will do just as well.

I shan't go into "break label;" too much, but many people believe it violates structured programming, which is why you don't find it in most of the books. Even Deitel and deitel have relegated it to an appendix on the CD only.
 
john cahil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please correct me if I am wrong but can we put rest of lines in to catch block also
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin's approach will be pretty short and sweet in a for loop instead of the label bit. Think about the poor reader coming along next week or next year to figure out why there is a loop with a switch like that.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But after i handle the exception is there any way of returning the control back to line 3.

No this is one of many defects of the language that makes the use of exceptions inappropriate. Other languages permit you to resume after a signal - Java doesn't. There are better alternatives to "exceptions".
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:

No this is one of many defects of the language that makes the use of exceptions inappropriate. Other languages permit you to resume after a signal - Java doesn't. There are better alternatives to "exceptions".


While I am sure that pk kumar is in awe of your java savvy and intellect, let's try and refrain from making things more complex that what they really are. This is the beginner forum after all.

Thanks.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PK, there are some possible solutions, depending on the details of the problem. If you show us more of your code - for example the actual lines that you need to be in individual try blocks - I'm sure we can be much more effective in helping.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:


While I am sure that pk kumar is in awe of your java savvy and intellect, let's try and refrain from making things more complex that what they really are. This is the beginner forum after all.

Thanks.


I agree entirely, hence I made the effort to respond with a simple, non-complex, sensible answer. That it violates common doctrine is not disputed.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:

There are better alternatives to "exceptions".


Such as?

Let's say that a program is trying to open a file. The file for whatever reason does not exist without using exceptions, how does one not only get around a checked exception compiler error, but handle the fact that the file may not exist?

Or get around say the very good possibility that user input is not correct and needs a integral value, not a string without exceptions.
[ August 13, 2006: Message edited by: Rusty Shackleford ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rusty Shackleford:
Let's say that a program is trying to open a file. The file for whatever reason does not exist without using exceptions, how does one not only get around a checked exception compiler error, but handle the fact that the file may not exist?


Tony is answering such questions regularly - and yes, there are alternatives. But I think that answering it here would be quite off topic. Please either search the forums, or start a new thread for the discussion. Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!