• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Catch and rethrow exception, with nothing else?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm seeing the following:



Is there any benefit to this whatsoever? Is there some subtle reason for catch and rethrow w/o any other processing?

Thanks!
Batkins61
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can put a breakpoint for your debugger at the "throw".

It's easier to modify that code to log the error at that point.

There's probably other advantages, but nothing outstandingly great.

 
B Atkins
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You can put a breakpoint for your debugger at the "throw".

It's easier to modify that code to log the error at that point.

There's probably other advantages, but nothing outstandingly great.



So other than as a placeholder or a debugging aid, there's no benefit? That's what I assumed, but I wasn't sure.

I wonder if the compiler optimizes it out?

Thanks!
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose it could be optimized out, but I doubt this sort of code occurs often enough for anyone to have bothered writing an optimization that will handle it. I could be wrong, of course.

One other effect I can think of occurs if the catch block is followed by another, more general catch block:

In this example, the final catch block will catch any exception except a MyException, which will be rethrown out of the try/catch.

Offhand, I can't think of a good example where code like this seems particularly useful. I think I might have had cause to do something like this once or twice in my life, but at the moment I can't remember the context of why I thought such a thing would be desirable. Still, this is at least an example where the first catch block does have an effect. In case you ever find yourself needing something like this.
 
B Atkins
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes, I've definitely seen and used that case; handling specific exceptions differently. In the cases I'm most familiar with, the specific exception is rethrown and the more general exception is wrapped in another exception and then thrown (often wrapped in the exception that was ahead of it in the catch list). This wasn't that situation.

I think it may well have been a left over debugging catch, so a breakpoint could be placed there before the method went out of scope.

Thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic