*
The moose likes Beginning Java and the fly likes Exception Handling code inside a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling code inside a loop" Watch "Exception Handling code inside a loop" New topic
Author

Exception Handling code inside a loop

Abhishk Singh
Ranch Hand

Joined: Aug 19, 2010
Posts: 43
I have a scenario in which I have to put run some block of code inside a loop which runs for many records , say 10000.
Code inside the loop is exception prone and needs to be put inside a try /catch block.

If I put the entire loop inside the try/catch block , and an exception occurs at any iteration , the loop execution stops and the program jumps to the exception handling block outisde the loop.
In my scenario the requirement is such that if exception comes in any of the iteration, the loop should continue and other records should be processed by the block of code inside the loop. So I need to put the exception handling code inside the loop.
Please suggest if there are some standrds violation/performance-hit in placing the try/catch block inside a loop.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

If that's what your program has to do, then that's how you have to program it. With regards to your worry about performance: Your primary concern should be that the program functions correctly; performance should only be a secondary concern. (Because a program that runs fast, but that is incorrect, is useless!).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
I would have thought there is little difference in performance between a loop surrounding a try-catch and a try-catch surrounding a loop.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Try this bit of crappy code:-java TryDemo 1234567
java TryDemo 1234567890
javap -c TryDemo
Karthik Jayachandran
Ranch Hand

Joined: Feb 18, 2009
Posts: 88

...one more option, you can put the Try-Catch block inside the loop and in the Catch block, mention continue keyword
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Karthik Jayachandran wrote:...one more option, you can put the Try-Catch block inside the loop and in the Catch block, mention continue keyword
What difference will that make?
Karthik Jayachandran
Ranch Hand

Joined: Feb 18, 2009
Posts: 88

Campbell Ritchie wrote:
Karthik Jayachandran wrote:...one more option, you can put the Try-Catch block inside the loop and in the Catch block, mention continue keyword
What difference will that make?


...it's just an example to tryout, as per OP requirements (the loop should continue to process other records even after exception).
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Peter Haggar, a JavaRanch old-timer, addresses the question of try-catch inside or outside loops in his book "Practical Java" (see Praxis 23 if you have the book). Peter also goes over an analysis of the generated bytecode. Basically, he says that with Just-In-Time (JIT) compilation, there is virtually no difference in the performance. However, with JIT off, the try-catch inside a loop suffers a ~20% performance hit because of the overhead of setting up a try-catch repeatedly. You could argue that most code will be compiled with optimization but it's still interesting to note the difference.


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7512
    
  18

abhishek singhania wrote:In my scenario the requirement is such that if exception comes in any of the iteration, the loop should continue and other records should be processed by the block of code inside the loop. So I need to put the exception handling code inside the loop.
Please suggest if there are some standrds violation/performance-hit in placing the try/catch block inside a loop.

Well, my first question is: what do you want to do if there is an error? Ignoring it completely is generally not advisable; but otherwise, my advice would be the same as Karthik's: put a continue at the end of your catch block.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Karthik Jayachandran wrote: . . . ...it's just an example to tryout, as per OP requirements (the loop should continue to process other records even after exception).
But what will the difference be if yo uomit the keyword continue?
Abhishk Singh
Ranch Hand

Joined: Aug 19, 2010
Posts: 43
Thank you all for the inputs!
For the requirement I am working on, the exception handling code will be inside the loop but the try block will enclose all the code inside the loop. So whether or not I add the continue statement, the loop will continue after the exception.
Also to following,
Well, my first question is: what do you want to do if there is an error?

The exception handling code will log the exception to a log file.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7512
    
  18

abhishek singhania wrote:For the requirement I am working on, the exception handling code will be inside the loop but the try block will enclose all the code inside the loop.

Ah. Well that's not what you said at the start ("Code inside the loop is exception prone and needs to be put inside a try /catch block"); and to be honest, I'm not sure if you can do what you want without making your code quite tortuous. Why not just do:Indeed, I'd be tempted to put the body of the loop into a method.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Aaaaaaaaaaaah. I see whatdifference continue would make. I thought all the code in the loop was inside the try, in which case continue would not have made any difference.
Abhishk Singh
Ranch Hand

Joined: Aug 19, 2010
Posts: 43
Winston Gutkowski wrote:
abhishek singhania wrote:For the requirement I am working on, the exception handling code will be inside the loop but the try block will enclose all the code inside the loop.

Ah. Well that's not what you said at the start ("Code inside the loop is exception prone and needs to be put inside a try /catch block"); and to be honest, I'm not sure if you can do that without making your code quite tortuous. Winston



The statement
abhishek singhania wrote:For the requirement I am working on, the exception handling code will be inside the loop but the try block will enclose all the code inside the loop.

was written after looking at all the suggestions in this thread. Initially the try block was entirely outside the loop only.But final solution that I am going with after all the suggestions is:


Thanks
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7512
    
  18

abhishek singhania wrote:But final solution that I am going with after all the suggestions is:

Well I hope for your sake that it works, because I can think of several scenarios in which it would run forever (or at least until it fills up your error log) - the most obvious one being that you have a genuine I/O problem - because you're basically ignoring the exception.

Ask yourself this: is your exception a genuine error, or does it simply indicate something recoverable, like a formatting issue?
If it's the latter, I'd be tempted to use logic to discover it rather than an Exception; and if I did use an Exception, I'd make it a custom one, and ONLY have my loop check for that.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
The questions in this thread have all been a bit vague. It was only, “what happens if I suffer an exception?”, not about a particular exception in particular circumstances. If the questions had been more specific, the answers would have been much better.
Abhishk Singh
Ranch Hand

Joined: Aug 19, 2010
Posts: 43
Campbell Ritchie wrote:The questions in this thread have all been a bit vague. It was only, “what happens if I suffer an exception?”, not about a particular exception in particular circumstances. If the questions had been more specific, the answers would have been much better.


To be more specific, I am working on a Java Class which runs on IBM Websphere Process Server(WPS) and uses its Human Task and Business Flow Manager APIs.
The code searches for specific PROCESS/TASK Instances and suspends them. If a processes has been suspended once and the code tries to suspend it again, the code throws an exception. Now it is possible that out of 10000 processes, 1-2% have already been suspended manually using BPC tool (tool to perform administrative tasks on Process/Task instances on WPS).

Another problem is that the query Iam using (which I can not change) to look for the processes to be suspended, may fetch the same result twice. Now if the same process/task appears again in the resultset and code tries to suspend it, an exception occurs. In such cases, the exception can be ignored.
As an alternative, I can check a process state(whether it is already suspended or not) and then attempt to suspend it.
Abhishk Singh
Ranch Hand

Joined: Aug 19, 2010
Posts: 43
Winston Gutkowski wrote:
Well I hope for your sake that it works, because I can think of several scenarios in which it would run forever (or at least until it fills up your error log)
Winston


Request you to let me know about the error scenarios you mentioned.
As my post above, in this case I can ignore the exception but please suggest if the logging of exception can create problems then I can attempt adding code to avoid/minimize exception cases.
It will help if you can provide me a link to resources where I can find information regarding such erroneous scenarios.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7512
    
  18

abhishek singhania wrote:Request you to let me know about the error scenarios you mentioned.

I already did - a genuine I/O error.

As my post above, in this case I can ignore the exception but please suggest if the logging of exception can create problems then I can attempt adding code to avoid/minimize exception cases.

Well according to your previous post:
"To be more specific, I am working on a Java Class which runs on IBM Websphere Process Server(WPS) and uses its Human Task and Business Flow Manager APIs. The code searches for specific PROCESS/TASK Instances and suspends them. If a processes has been suspended once and the code tries to suspend it again, the code throws an exception."
it would seem that what you're trying to "ignore" (or log) is a SuspendedTwiceException.

Surely, it would be smarter to simply check if the process has already been suspended? Exceptions are usually thrown to indicate an error, so it would seem rather silly for the process not to have an isSuspended() or isRunning() method.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling code inside a loop
 
Similar Threads
declare or handle
User-defined exceptions
Exceptions - Basics
Populate Backing Bean Exception Handling
Loops and try...catch