Is there a difference between “throw” and “throw ex”?

前端 未结 10 2391
夕颜
夕颜 2020-11-22 01:08

There are some posts that asks what the difference between those two are already.
(why do I have to even mention this...)

But my question is different in a way

10条回答
  •  遥遥无期
    2020-11-22 01:48

    int a = 0;
    try {
        int x = 4;
        int y ;
        try {
            y = x / a;
        } catch (Exception e) {
            Console.WriteLine("inner ex");
            //throw;   // Line 1
            //throw e;   // Line 2
            //throw new Exception("devide by 0");  // Line 3
        }
    } catch (Exception ex) {
        Console.WriteLine(ex);
        throw ex;
    }
    
    1. if all Line 1 ,2 and 3 are commented - Output - inner ex

    2. if all Line 2 and 3 are commented - Output - inner ex System.DevideByZeroException: {"Attempted to divide by zero."}---------

    3. if all Line 1 and 2 are commented - Output - inner ex System.Exception: devide by 0 ----

    4. if all Line 1 and 3 are commented - Output - inner ex System.DevideByZeroException: {"Attempted to divide by zero."}---------

    and StackTrace will be reset in case of throw ex;

提交回复
热议问题