0

I've got a new project. Every time you dealing with somebody else code it's an adventure.

Here is what I found:

try
{
   .....
}
catch (InvalidOperationException e) {
    throw e;
}
catch (Exception e)
{
    throw;
}

Anybody has an idea why?

PS Thanks everybody. It really helps. Here are some good sources that you recommended:

Why catch and rethrow an exception in C#?

http://msdn.microsoft.com/en-us/library/0yd65esw.aspx

http://msdn.microsoft.com/en-us/library/ms229005.aspx

Community
  • 1
  • 1
GenZiy
  • 1,427
  • 2
  • 15
  • 20
  • 2
    because they want to test you :) – Simone May 11 '11 at 12:38
  • 2
    `catch (Exception up) { throw up; }` – Oded May 11 '11 at 12:39
  • @Oded: [`//ha ha`](http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered/549611#549611) – BoltClock May 11 '11 at 12:40
  • another reason this is poor practice is that catching and re-throwing an exception in the same fashion as the InvalidOperationException is adding bumf into the stack trace – jcvandan May 11 '11 at 12:46

6 Answers6

3

Because whoever has written this doesn't have any understanding of how exceptions work in .NET.

If you don't do anything with an exception, don't catch it.

The code you posted would better be written as:

.....
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • While I agree that the code makes little sense, it couldn't be rewritten like that as `throw e;` actually modifies the stack trace captured in the instance (probably not the intention but still ...) – Brian Rasmussen May 11 '11 at 12:44
  • @Brian - ok... better _written_ as. – Oded May 11 '11 at 12:47
2

Makes no sense whatsoever to me, but not for the reason you might think.

Catching an exception is not the same thing as handling it. This try/catch block does no handling at all. I think a better, more honest, less verbose solution would have been to remove the try/catch and let the exceptions bubble up to where they can/should be dealt with.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

The real danger of this (other than being completely useless...) is that it modifies the call stack. Others have briefly mentioned it in comments, but it deserves to be called out specifically.

When you have throw ex;, the previous call stack is blown away and replaced with the call stack at the point where throw ex; is called. You almost never want to do this. I will often catch an exception, log it, then rethrow the exception. When doing that, you want to just use throw;. This will preserve the original stack trace.

Tim Coker
  • 6,484
  • 2
  • 31
  • 62
1

i guess the application wants the outer method calling this to catch the exception instead but this is not the way it should be done, do check these references as they are somewhat similar

Why catch and rethrow an exception in C#?

http://winterdom.com/2002/09/rethrowingexceptionsinc

Community
  • 1
  • 1
Rejinderi
  • 11,694
  • 2
  • 31
  • 40
  • Thank you. It maks me feel better. http://msdn.microsoft.com/en-us/library/ms229005.aspx http://msdn.microsoft.com/en-us/library/0yd65esw.aspx – GenZiy May 11 '11 at 12:57
1

It is possible that they wanted to implement different handling for InvalidOperationException and other types of exceptions, so they wrote this code as a stub. But then this idea was abandoned so you see this code artifact.

Dmitry
  • 3,069
  • 1
  • 17
  • 26
0

you can improve this code by adding logging.

after every catch, log the information that you throw.

there are some opensource code available for logging.