• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to continue after an Exception?  RSS feed

 
irshad ahmed
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to continue a Java program after if it catches and deals with an exception?
For example, say I am processing 1000 records from a file, one at a time. If the 6th record causes a Java Exception, I want to be able to skip it and continue processing the remaining 994 records.
Is this possible with Java Exception handling? If so, how?
Thanks,
Irshad
[ April 06, 2004: Message edited by: irshad ahmed ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Irshad-
I'm glad you're trying to learn more about Java and how it can help you to deal with problems (and I think you're on the right track with Exception Handling). But please don't post the same thread in multiple forums. It's very confusing and wastes peoples time when they answer a question thats already been answered elsewhere.
I'll leave this thread open to continue the discussion. Thanks.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
possible approach
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this:

From here, you can report to the user these records were bad, you can use an alternate approach to try to process them, log that they didn't get processed, etc, etc...basically, take whatever action you want to take in response to a bad record. You could write a handleBadRecord(Record r) method that you call from the catch block that does whatever you want.
Another common tactic is to create your own exception, and if badRecords.size()>0 once you're done processing, throw that exception with the list of the bad ones to the caller and let them decide what to do.
sev
[ April 11, 2004: Message edited by: sever oon ]
 
Aleksey Matiychenko
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest couple of things.
First see if you really expect a java exception or a boolean condition.
If you can avoid java exception I would suggest doing that because using try/catch in a loop is very inefficient.
Second I agree with the previous post about creating your own exception and adding information into that exception within the loop.
At the end you want to throw your own exception.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
irshad,
As you have time, let me suggest a few free on-line resources I've used to better understand exception handling in Java.
  • chapter 9 of Bruce Eckel's Thinking In Java Book
  • chapter 9 of David J. Eck's Introduction to Programming Using Java
  • chapters 80 & 81 of Bradley Kjell's Introduction to Computer Science using Java
  • Dick Baldwin's The Essence of OOP using Java, Exception Handling Article and
  • The Handling Errors with Exceptions Lesson of Sun's Java Tutorial
  • Then, when you're ready to have some fun, take a look at this past JavaRanch conversation as well as http://c2.com/cgi/wiki?IlluminateTheMainline
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!