$0.48

Exceptions difference between throw; and throw e;(where e is an excetion)

Ask a question+
1

Can you explain me Exceptions difference between throw; and throw e; (where e is an excetion)

add comment

1 Answer

1
  • throw ex resets the stack trace (so your errors would appear to originate from HandleException)

  • throw doesn’t - the original offender would be preserved.

static void Main(string[] args)
{
    try
    {
        Method2();
    }
    catch (Exception ex)
    {
        Console.Write(ex.StackTrace.ToString());
        Console.ReadKey();
    }
}

private static void Method2()
{
    try
    {
        Method1();
    }
    catch (Exception ex)
    {
        //throw ex resets the stack trace Coming from Method 1 and propogates it to the caller(Main)
        throw ex;
    }
}

private static void Method1()
{
    try
    {
        throw new Exception("Inside Method1");
    }
    catch (Exception)
    {
        throw;
    }
}
add comment

Your Answer