有间及QUOT的差值;抛出"和"抛出前"?抛出、差值、有间、QUOT

2023-09-02 11:29:34 作者:我向来孤僻成性却念你好深

有一些帖子,询问这两位之间的差异已经。 (为什么我连提这个......)

There are some posts that asks what the difference between those two are already. (why do I have to even mention this...)

但我的问题是,在某种程度上,我叫扔恩的另一个错误的神一样的的处理方法。

But my question is different in a way that I am calling "throw ex" in another error god-like handling method.

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            // something
        }
        catch (Exception ex)
        {
            HandleException(ex);
        }
    }

    private static void HandleException(Exception ex)
    {
        if (ex is ThreadAbortException)
        {
            // ignore then,
            return;
        }

        if (ex is ArgumentOutOfRangeException)
        {
            // Log then,
            throw ex;
        }

        if (ex is InvalidOperationException)
        {
            // Show message then,
            throw ex;
        }

        // and so on.
    }
}

如果尝试和放大器;捉中的,然后我会用用扔; 重新抛出错误。 但是,在上述的简单求code,所有的异常通过 HandleException

If try & catch were used in the Main, then I would use throw; to rethrow the error. But in the above simplied code, all exceptions go through HandleException

确实罚球前; 具有相同的效果与调用内调用时 HandleException

Does throw ex; has the same effect as calling throw when called inside HandleException?

推荐答案

是的,是有区别的;

抛出前复位堆栈跟踪(让你的错误似乎从起源 HandleException 抛出不 - 原罪犯将是preserved throw ex resets the stack trace (so your errors would appear to originate from HandleException) throw doesn't - the original offender would be preserved.