• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catch and rethrow exception, with nothing else?  RSS feed

 
B Atkins
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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
 
C La
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!