dcsimg
== Exception overhead ==
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   joey_corleon
Posted On:   Thursday, October 10, 2002 11:01 AM

hi,


We often ear that use of exceptions are expensive in JAVA.

I unsderstand by this that they should used wisely - Ok

But does this also mean that the try {} block must be as small as possible or does the size of this block have no importance??
a.k.a If I have a serie of calls that might trow an exception but that thoswe calls (say 20 of them) are separated by some computing code that does not throw any exception, it it better to create a massive try block or is it better create 20 of them? - What is the impact???


Thanks

Re: == Exception overhead ==

Posted By:   Jeanne_Boyarsky  
Posted On:   Thursday, October 10, 2002 06:28 PM

It depends on what you are trying to do. If your later calls are dependent on the earlier ones, you should use one big try block. However, if the calls/computations are completely independent, you should use 20 of them.


Of course if the calls/computations are completely independent, they really don't belong in the same method. Also, exceptions aren't that expensive if they aren't thrown.

Re: == Exception overhead ==

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, October 10, 2002 02:13 PM

As I see it, the smaller the try block, the more granularity you have, of writing more specific handler code in the catch block, for the specific code in the try block.



And with smaller, more specific try blocks, you can be confident that you have safely completed the code in your earlier try block, when debugging.



But then, the larger the try block, the less messy the code is.



So it is a trade-off, to balance these two factors, in your particular situation.



You might want to group a few of your exception-throwing calls (with the intervening safe code) into a small number of try blocks, by how they are related and how it makes your code better, as a compromise, between one huge try block around the whole method on the one hand, or all individual try blocks for each call, on the other hand.

About | Sitemap | Contact