在finally块中获取抛出异常

时间:2010-01-14 08:37:41

标签: c# exception-handling finally

有没有办法,如何获得当前抛出的异常(如果存在)?

我希望减少代码量,并为任务应用一些重用:

Exception thrownException = null;
try {
    // some code with 3rd party classes, which can throw unexpected exceptions
}
catch( Exception exc ) {
    thrownException = exc;
    LogException( exc );
}
finally {
    if ( null == thrownException ) {
        // some code
    }
    else {
        // some code
    }
}

并将其替换为此代码:

using( ExceptionHelper.LogException() ) {
    // some code with 3rd party classes, which can throw unexpected exceptions
}
using( new ExceptionHelper { ExceptionAction = ()=> /*some cleaning code*/ } ) {
    // some code with 3rd party classes, which can throw unexpected exceptions
}

public class ExceptiohHelper : IDisposable {
    public static ExceptionHelper LogException() {
        return new ExceptionHelper();
    }

    public Action SuccessfulAction {get; set;}
    public Action ExceptionAction {get; set;}

    public void Dispose() {
        Action action;
        Exception thrownException = TheMethodIDontKnow();
        if ( null != thrownException ) {
            LogException( thrownException );
            action = this.ExceptionAction;
        }
        else {
            action = this.SuccessfulAction;
        }

        if ( null != action ) {
            action();
        }
    }
}

这种情况是否可行?

由于

3 个答案:

答案 0 :(得分:8)

这个想法是你在catch块中处理异常......

那就是说,Exception是一个引用类型,所以你总是可以在try范围之外声明一个Exception变量......

Exception dontDoThis;
try
{
    foo.DoSomething();
}
catch(Exception e)
{
    dontDoThis = e;
}
finally
{
    // use dontDoThis...
}

答案 1 :(得分:4)

您对以下内容有何看法?如果将其更改为“如何使用更多控件运行某些代码?”,而不是将问题视为“如何获取最后一个异常?”,该怎么办?

例如: 您可以使用ActionRunner而不是ExceptionHelper。

public class ActionRunner
{
    public Action AttemptAction { get; set; }
    public Action SuccessfulAction { get; set; }
    public Action ExceptionAction { get; set; }

    public void RunAction()
    {
        try
        {
            AttemptAction();
            SuccessfulAction();
        }
        catch (Exception ex)
        {
            LogException(ex);
            ExceptionAction();
        }
    }

    private void LogException(Exception thrownException) { /* log here... */ }
}

假设只有AttemptAction在调用之间变化,它至少会让你重用一下SuccessfulAction和ExceptionAction。

var actionRunner = new ActionRunner
{
    AttemptAction = () =>
    {
        Console.WriteLine("Going to throw...");
        throw new Exception("Just throwing");
    },
    ExceptionAction = () => Console.WriteLine("ExceptionAction"),
    SuccessfulAction = () => Console.WriteLine("SuccessfulAction"),
};
actionRunner.RunAction();

actionRunner.AttemptAction = () => Console.WriteLine("Running some other code...");
actionRunner.RunAction();

答案 2 :(得分:3)

如果您希望捕获意外异常,则应该处理UnhandledException。你应该只捕获你想要处理的较低级别的异常(不仅仅是记录),否则你应该让它们冒泡并被更高级别捕获,或者如我之前在UnhandledException方法中提到的那样。