• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Performance Differences in Exception Handling

 
J Kilgrow
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bit of a hypothetical question that I've been noodling on regarding performance in exception handling. Take a peek and I would love to hear what everyone thinks.

Basically, I am thinking about two approaches to exception handling.

Approach 1:


Approach 2:


Try to ignore syntax, refactoring issues, etc. The essence of the question is, "Is it better performance to catch an individual exception or is it better performance to catch Exception and then parse it looking for specific exceptions?"

Thanks!
 
Paul Clapham
Sheriff
Posts: 21588
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to know what I think? I don't think it makes any difference, performance-wise. And as a developer, it would have to make a pretty big difference before I would decide I was forced to use Approach 2. Basically you'd have to pry Approach 1 out of my cold dead hands.

But that's only what I think. Developers are notoriously bad at guessing in advance the performance of a particular piece of code, let alone judging how meaningful performance differences are. So if you think there's an important difference then you should write some code which tries to measure that difference. If it was me I would just use Approach 1 and do nothing else until my attention was drawn to significant performance problems which could be traced to that code.
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Kilgrow wrote:The essence of the question is, "Is it better performance to catch an individual exception or is it better performance to catch Exception and....?"

If that's the question, then I suspect it's the wrong one, because performance should NOT be what concerns you - at least, not at this stage.

The first question should be: Which is better? And in general I agree with Paul, #1 is, for all the reasons he mentions (and a few more besides).

However, another question you should ask yourself is: What is the intent of this code?

AFAICS, it simply converts whatever Exception is thrown to a runtime one; and if that's the case, both are overwritten, IMO.

For example, using your example #1, you could achieve an equivalent result with:HIH

Winston
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Additionally, we are talking about exception conditions here... which technically, if your application is working correctly, should rarely (if ever) happen.


And trust me ... if you are getting exceptions, your users will *not* be complaining about the performance of getting those exceptions... the complaints will be on something else !! ...

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic