有间及QUOT的差值;抛出"和"抛出前"? [英] Is there a difference between "throw" and "throw ex"?

查看:202
本文介绍了有间及QUOT的差值;抛出"和"抛出前"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于有间及QUOT的差值;抛出"和"抛出前"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆