jQuery in Action, 2nd edition*
The moose likes Performance and the fly likes Best Pratice : catch CheckedException late Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Best Pratice : catch CheckedException late " Watch "Best Pratice : catch CheckedException late " New topic
Author

Best Pratice : catch CheckedException late

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,


This is on to Best Pratices in java

This point is specified :


1. You should catch CheckedException late in a catch block

Please tell what the author mean to mention here , how can this be a best pratice ??


Save India From Corruption - Anna Hazare.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9315
    
109

This is on to Best Pratices in java


Is that a book or this question related to Performance?


[My Blog] [JavaRanch Journal]
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

The Book name is Best Performance in Java

In taht book, this point is mentioned .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Its an odd statement that I'm not sure I understand. Can you provide a bit more context? Perhaps an example if its included in this book you reference?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Amazon.com doesn't hava any listing for a book called "Best Performance in Java". Are you sure that's the correct title? Who is the author?

I have a hard time imagining any circumstance where the decision of where to catch an exception should be based on performance considerations. It's just not a relevant issue. Such decisions should be based on: where should the control flow go? If an exception occurs, which subsequent actions should be skipped as no longer relevant, and which should be attempted anyway? Forget about performance here - it doesn't matter.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15628
    
  15

It's not generally mentioned in Java, but the very use of try/catch was a little bit controversial in C++. When you throw an exception, a stack unwinding process occurs and it can be a fair amount of overhead. I think there was also a builtin overhead for setting up a throwable environment even if the exception was never thrown. Although most people considered it a lot cleaner than the older setjmp/longjmp alternative, some people warned against the penalties involved.

Actually, the title "Best Performance in Java" sounds a bit informal and/or foreign, and from the sound of it, more likely to be about how to use Java most effectively than literally about runtime performance.


Customer surveys are for companies who didn't pay proper attention to begin with.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Yes, I was talking about the decision of where to catch an exception, given that it's being thrown. The decision of whether to throw an exception in the first place is a bit different, and yes it can have an effect on performance. Even if the performance effect ultimately doen't matter in most cases, at least it can be present.
Lakshman Arun
Greenhorn

Joined: Sep 24, 2008
Posts: 19
Hi,

Its good to catch the exception as close to its origin(provided the caller knows how to deal with that exception). I am wondering how throwing an checked exception is one of best practice(to me it looks very odd). Allowing the exception to propagate unnecessarily to the top of the call hierarchy lead to performace issues.

Regards
Lakshmanan
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Um, why do you say it leads to performance issues? I don't see it. If anything, the methods complete faster because they do less. The performance impact comes primarily from creating the exception (it takes time to populate the stack trace) and (optionally) from logging the stack trace. Throwing and catching are pretty trivial. All these are independent of where you catch the exception, as far as I can tell.

The other parts of the question would be better asked in another forum, I think. They are independent of performance issues, and have been discussed many times, as well.
Lakshman Arun
Greenhorn

Joined: Sep 24, 2008
Posts: 19
The reason for performance issue is due to filling up the stack, whenever an exception is rethrwon the stack will be added with more details.

Regards
Lakshmanan
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
Ah, OK. That's not a function of where you catch it, I think, but of how many times you catch and rethrow it. Well, assuming you are creating a new exception each time and wrapping the old one. If you allow an exception to propagate up the hierarchy without catching and rethrowing, it will have no performance impact. Or if you catch and rethrow the same exception, with no rewrapping, it will have negligible impact. Catching, rewrapping and throwing can have an impact, true.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best Pratice : catch CheckedException late
 
Similar Threads
Checked Exceptions should be catched Late in the Application .
System.out.println is Wrong??
Is ServletException an unchecked Exception
Code alternative
Handling RuntimeException in Java