简体   繁体   中英

Catch Exception, add data, and rethrow it

I have the following code:

try
{
   OnInitialize();
}
catch (PageObjectLifecycleException exception)
{
   exception.OldLifecycleState = CurrentLifecycleState;
   exception.RequestedLifecycleState = LifecycleState.Initialized;
   throw exception;
}

I catch an exception, add some more data to it, and rethrow it. Resharper warns me (correctly) that a rethrow is possibly intended and suggests changing it to:

throw;

But I'm wondering: Will this correctly rethrow the modified exception or the unmodified original one?

Edit : In response to the "Try it and see" comments: I am new to C#, comming from C++. In C++ you often find undefined behaviour in corner cases like this and I am interested in whether what I want is really how it officially works.

You can add the extra information into data and re-throw the exception with throw so it maintains its original form and the callstack

try
{
   ...
}
catch (PageObjectLifecycleException exception)
{
   exception.Data.Add("additional data", "the additional data");
   throw;
}

I know the answer's already been chosen but here's a little more info on the subject.

try {
    // code
}
catch(Exception e) {
    throw e;
}

The above code when compiled into IL will produce a call to throw passing a reference to the handled exception as an argument. As you are probably aware, you can call throw from anywhere in your code to raise an exception.

try {
    // code
}
catch(Exception e) {
    throw;
}

The above code when compiled into IL will produce a call to rethrow . This is different to throw as rethrow is used to signal that the block in which the exception was handled has for some reason decided not to handle it and therefore, responsibility should be offered to a higher order catch block (next one up).

The rethrow method preserves the current call stack trace so that the origin of the exception can be tracked down. However, the throw method starts a new call stack trace. I think this makes sense once you understand what the two methods are meant to be used for.

In my experience you use throw exception; when you want to throw an exception for some reason (eg validation of an object failed) and you would use throw; in a catch statement after you've performed some logging (ie while you still have access to useful information in the object that failed validation before passing exception handling responsibilities to a higher level).

In your example I would suggest that if you need to add more information to an exception you have a case for creating an entirely new exception and raising it. So you would use the throw exception; method where "exception" is a new exception containing the extra information and the originally thrown exception.

Hope that helps!

James

It will throw the reference to the modified exception.

However I am not sure whether this is good programming style. Consider creating a new exception and add the PageObjectLifecycleException as its inner exception. This way the handling code can be sure whether it has the correct additional information or not.

当前的异常被重新抛出,虽然我不确定你的模式是一个非常好的和可维护的模式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM