相关疑难解决方法(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万
查看次数

捕获非类型的异常

捕获不属于以下类型的异常时是否有区别:

try
{
    ...
}
catch (TaskCanceledException)
{
    throw;
}
catch (Exception exception)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

和 :

try
{
    ...
}
catch (Exception exception) when (!(exception is TaskCanceledException))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

c#

8
推荐指数
1
解决办法
2249
查看次数

标签 统计

c# ×2

.net ×1

exception ×1

exception-handling ×1