相关疑难解决方法(0)

"throw"和"throw ex"之间有区别吗?

有些帖子询问这两者之间的区别是什么.
(为什么我还要提这个...)

但我的问题是不同的,我称之为"抛出前"在另一个错误的神像处理方法.

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

如果try & catch用于Main,那么我会throw; …

.net c# exception-handling exception

414
推荐指数
8
解决办法
18万
查看次数

66
推荐指数
2
解决办法
2万
查看次数

在C++中,"throw"和"throw ex"之间有区别吗?

我想问这个问题(也在这里),但这次是关于C++的.

C++之间的区别是什么?

try { /*some code here*/}
catch(MyException& ex)
{ throw ex;} //not just throw
Run Code Online (Sandbox Code Playgroud)

try {  /*some code here*/}
catch(MyException& ex)
{ throw;} //not throw ex
Run Code Online (Sandbox Code Playgroud)

它只是在堆栈跟踪中(在C++中,在任何情况下都不是C#或Java中的标准)?

(如果它有任何区别,我使用MSVS 2008.)

c++ exception-handling

18
推荐指数
4
解决办法
5658
查看次数

Resharper异常重新抛出可能是有意的

考虑一下这种方法(原谅Chuck Norris幽默的悲惨尝试:)):

public class ChuckNorrisException : Exception
{
    public ChuckNorrisException()
    {
    }

    public ChuckNorrisException(string message)
        : base(message)
    {
    }

    public ChuckNorrisException(string message, Exception cause)
        : base(message, cause)
    {
    }

    protected ChuckNorrisException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
    }
}

static void ExceptionTest(double x)
{
    try
    {
        double y = 10 / x;
        Console.WriteLine("quotient = " + y);
    }
    catch (Exception e)
    {
        e = e is DivideByZeroException ? new ChuckNorrisException("Only Chuck Norris can divide by 0!", e) :
            e; …
Run Code Online (Sandbox Code Playgroud)

c# resharper exception

15
推荐指数
1
解决办法
9236
查看次数

标签 统计

c# ×3

exception ×3

.net ×2

exception-handling ×2

c++ ×1

resharper ×1