Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best Pratice : catch CheckedException late

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is on to Best Pratices in java


Is that a book or this question related to Performance?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Book name is Best Performance in Java

In taht book, this point is mentioned .
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic