Is there any real world reason to use throw ex?

Posted by Michael Stum on Stack Overflow See other posts from Stack Overflow or by Michael Stum
Published on 2010-12-29T08:51:56Z Indexed on 2010/12/29 9:53 UTC
Read the original article Hit count: 223

Filed under:
|
|

In C#, throw ex is almost always wrong, as it resets the stack trace.

I just wonder, is there any real world use for this? The only reason I can think of is to hide internals of your closed library, but that's a really weak reason. Apart from that, I've never encountered in the real world.

Edit: I do mean throw ex, as in throwing the exact same exception that was caught but with an empty stacktrace, as in doing it exactly wrong. I know that throw ex has to exist as a language construct to allow throwing a different exception (throw new DifferentException("ex as innerException", ex)) and was just wondering if there is ever a situration where a throw ex is not wrong.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET