views:

1388

answers:

4

This is a follow-up question to Is there a difference between “throw” and “throw ex”?

is there a way to extract a new error handling method without resetting the stack trace?

[EDIT] I will be trying both "inner method" and another answer provided by Earwicker and see which one can work out better to mark an answer.

+17  A: 

Yes; That's what the InnerException property is for.

catch(Exception ex)
{
    throw new YourExceptionClass("message", ex);
}

This will allow you to add your own logic, then throw your own exception class. The StackTrace of the YourExceptionClass instance will be from within this code block, but the InnerException will be the exception you caught, with the StackTrace it had before.

Adam Robinson
+2  A: 

You don't want to create a new exception with the original stack trace. That is misleading, since that stack trace did not create the new exception.

You can, however, put the original exception in your new exception as an "InnerException". Would that do what you are looking for?

Brian Genisio
I want to emulate "throw;" within an extracted error handling method.
Sung Meister
+9  A: 

Not sure if you mean that, but my suggestion in your other question was addressing this.

If your handler returns a boolean whether the exception was handled or not, you can use this in your catch clause:

catch (Exception ex) {
  if (!HandleException(ex)) {
    throw;
  }
}
Lucero
+marked as answer: seems like the easiest to trace error back to the source and easy to read. My "HandleException" method is named as "TryHandleRecoverableException", by the way.
Sung Meister
+2  A: 

Are you catching exceptions that you want to then filter more carefully, so you can then change your mind, decide not to handle them and so rethrow them?

If you want to be really careful about this, that's not really a good idea. It's better to never catch the exception in the first place. The reason is that a given try/catch handler should not take the decision to run nested finally blocks for exceptions that it is not expecting to see. For example, if there is a NullReferenceException, it's probably a very bad idea to continue executing any code as it will probably cause another such exception to be thrown. And finally blocks are just code, like any other code. As soon as an exception is caught for the first time, any finally blocks on the stack beneath the try/catch will be executed, by which time it's too late - another exception may be generated, and this means that the original exception is lost.

This implies (in C#) that you have to careful write out a separate catch handler for all the exception types you want to catch. It also implies that you can only filter by exception type. This is sometimes very hard advice to follow.

It should be possible to filter exceptions in other ways, but in C# it is not. However, it is possible in VB.NET, and the BCL itself takes advantage of this by having a small amount of code written in VB.NET so it can filter exceptions in a more convenient way.

Here's a detailed explanation, with a VB.NET code sample, from the CLR team blog.

And here's my two cents.

Daniel Earwicker
"Are you catching exceptions that you want to then filter more carefully, so you can then change your mind, decide not to handle them and so rethrow them?" -> Yes.
Sung Meister
"finally" code is always executed, no matter whether an exception occurred and how you handle the exception. I don't see the relation to the question...
Lucero
+1 for the link.. a useful thing that VB has and C# doesn't... strange. Maybe its because the C# designers want you to catch specific exceptions and design exception hierarchies correctly so that you dont have to catch and rethrow few specific derivations
Gishu
+1 Earwicker, your 2cents was really great. I would love to use "Exceptions" class somewhere in the code as well.
Sung Meister
@Lucero - not necessarily. Try handling AppDomain.UnhandledException and you will find that it fires _before_ finally blocks run, giving you the option to call Environment.FailFast before they do. But any intervening catch/rethrow will interfere with this capability.
Daniel Earwicker
The relation to the question is straightforward - Sung Meister IS doing the thing I asked about, and so my advice is "Don't do it that way".
Daniel Earwicker
@Gishu - yes I'm sure that's the reason. But unfortunately that requires predictive powers that class library designers don't have access to, even the BCL team itself! The whole history of SystemException vs. ApplicationException is very illuminating here.
Daniel Earwicker