简体   繁体   English

C# 异常处理 - 上次捕获块获取是否会重新抛出异常?

[英]C# Exception Handling - will last catch block fetch re-thrown Exception?

I'm wondering if this code will work in the way that after 10 retries CleanUp() will be called?我想知道这段代码是否可以在 10 次重试后调用CleanUp()的方式工作? Or will the throw new Exception() not get caught by the last catch block?或者throw new Exception()不会被最后一个 catch 块捕获?

public void Process()
{
    try
    {}
    catch (MyException e)
    {           
        int retries = GetRetries();
        if(retries > 10)
        {
            _logger.LogError("Retried event already {retries} times. Giving up.", retries);
            throw new Exception("Giving up!");
        }

        Process();

    }
    catch (Exception e)
    {
        CleanUp();
    }
}

No, it doesn't work like that.不,它不是那样工作的。 For any given try / catch block, only an exception originating in the try can be caught by the corresponding catch blocks.对于任何给定的try / catch块,只有源于try的异常才能被相应的catchcatch Exceptions thrown within catch blocks are not catchable by other catch blocks at the same level.catch块中抛出的异常不能被同一级别的其他catchcatch

finally blocks allow you to run code when control leaves a try / catch for any reason. finally块允许您在控件因任何原因离开try / catch时运行代码。 So if the end of your try already has a CleanUp then you would just change your final catch(Exception e) to finally and remove the CleanUp at the end of the try part.因此,如果您的try结束时已经有了CleanUp那么您只需将最终catch(Exception e)更改为finally并在try部分的末尾删除CleanUp

If, however, you only want to run CleanUp when control leaves the try / catch via an exception , you're not so lucky.但是,如果您只想在控制通过异常离开try / catch时运行CleanUp ,那么您就没有那么幸运了。 There is a fault clause in IL, which is finally , but only for exceptions, but that's not exposed in C#. IL 中有一个fault子句,它是finally ,但仅用于异常,但在 C# 中没有公开。

So if you need that, it's usually best to introduce an extra bool that indicates you reached the end of the try block without exception, and use that to make your CleanUp call conditional in the finally .因此,如果您需bool ,通常最好引入一个额外的bool指示您无一例外地到达了try块的末尾,并使用它使您的CleanUp调用在finally有条件。


Or you may simulate try / catch / fault by nesting try / catch blocks:或者你可以通过嵌套try / catch块来模拟try / catch / fault

try
{
    try
    {}
    catch (MyException e)
    {           
        int retries = GetRetries();
        if(retries > 10)
        {
            _logger.LogError("Retried event already {retries} times. Giving up.", retries);
            throw new Exception("Giving up!");
        }

        Process();

    }
}
catch
{
  //Act as fault
  CleanUp();
  throw;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 保留重新抛出的异常的类型 - Preserving the type of a re-thrown exception 查找没有通过RegEx设置内部异常的重新抛出的catch语句 - Find catch statements re-thrown that don't have the inner exception set via RegEx 为什么抛出的异常与catch块C#没有正确匹配 - Why thrown exception is not correctly matched with catch block C# C#异常处理最后在catch块之前阻塞 - C# exception handling finally block before catch block C# .NET:嵌套的 try catch 块未正确处理异常 - C# .NET: nested try catch block not handling exception properly 在C#中传播在finally块中抛出的异常而不丢失catch块中的异常的最佳实践是什么? - What is the best practice in C# to propagate an exception thrown in a finally block without losing an exception from a catch block? Selenium C# (NUnit) - 异常未在 catch 块中捕获,因为即使抛出异常也会执行 [onetimeteardown] - Selenium C# (NUnit)- Exception not caught in catch block as [onetimeteardown] executes even if exception thrown 在C#中,如果关联的catch块引发异常,是否可以强制控制通过finally块? - in C# is it possible to force control to pass through a finally block if an exception is thrown by an associated catch block? 在C#的Try块中,如果要在我的textbock中输入一个int,我想引发异常的catch部分吗? - In C# a Try block, the catch section i want a exception to be thrown if in my textbock a int is entered? 在 System.Threading.Timers TimerCallback 事件处理程序中捕获异常,然后重新抛出未发送回主线程 - Exception caught in System.Threading.Timers TimerCallback event handler and then re-thrown not sent back to main thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM