小编Sam*_*Sam的帖子

在.NET异常中保留原始StackTrace/LineNumbers

理解throw exthrow之间的区别,为什么在这个例子中保留了原始的StackTrace:

    static void Main(string[] args)
    {
        try
        {
            LongFaultyMethod();
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
        }
    }

    static void LongFaultyMethod()
    {
        try
        {
            int x = 20;
            SomethingThatThrowsException(x);
        }
        catch (Exception)
        {
            throw;
        }
    }

    static void SomethingThatThrowsException(int x)
    {
        int y = x / (x - x);
    }
Run Code Online (Sandbox Code Playgroud)

但不是在这一个:

    static void Main(string[] args)
    {
        try
        {
            LongFaultyMethod();
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.StackTrace);
        }
    }

    static void LongFaultyMethod()
    {
        try
        {
            int x = …
Run Code Online (Sandbox Code Playgroud)

.net exception line-numbers

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

标签 统计

.net ×1

exception ×1

line-numbers ×1