Ursprüngliche StackTrace / LineNumbers in .NET-Ausnahmen beibehalten

Den Unterschied zwischen verstehenEx werfen undwerfen, warum wird die ursprüngliche StackTrace in diesem Beispiel beibehalten:

    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);
    }

Aber nicht in diesem:

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

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

Das zweite Szenario erzeugt die gleiche Ausgabe wieEx werfen würde?

In beiden Fällen erwartet man die Zeilennummer, in der y initialisiert wird.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage