asp.net - Not finding any difference between throw vs throw ex in C# -


i trying difference between throw & throw ex. it, got number of links & given me enough of theory

summarizing above points difference:-

throw re-throws exception caught, , preserves stack trace. throw ex throws same exception, resets stack trace method.

so went ahead & created demo application same see difference in action.

namespace consoleapplication1 { class program {     static void main(string[] args)     {         calc c = new calc();         c.test();     } }  class calc {     public void test()     {         try         {             int = 10;             int b = 10;             int c = 10 / (a - b);         }         catch (exception ex)         {              throw; //throw statement          }     }  } } 

this gives me output as:-

unhandled exception: system.dividebyzeroexception: attempted divide zero. @ consoleapplication1.calc.test() in c:\kgn\personal\workspace\consoleapplication1\consoleapplication1\program.cs:line 31 @ consoleapplication1.program.main(string[] args) in c:\kgn\personal\workspace\consoleapplication1\consoleapplication1\program.cs:line 14

now replaced throw throw ex.

 class calc {     public void test()     {         try         {             int = 10;             int b = 10;             int c = 10 / (a - b);         }         catch (exception ex)         {              throw ex; // throw ex statement         }     } } 

this gives output as:-

unhandled exception: system.dividebyzeroexception: attempted divide zero. @ consoleapplication1.calc.test() in c:\kgn\personal\workspace\consoleapplication1\consoleapplication1\program.cs:line 31 @ consoleapplication1.program.main(string[] args) in c:\kgn\personal\workspace\consoleapplication1\consoleapplication1\program.cs:line 14

if see exception messages, both identical.

so difference??

i agree surely there difference why not finding,what point missing here??

the problem you're not seeing possible throw locations you're working close indistinguishable when stack trace collected.

try instead:

public void test() {   try   {     deeper();   }   catch (exception ex)   {      throw; //throw statement    } }  private static void deeper() {   int = 10;   int b = 10;   int c = 10 / (a - b); } 

the throw; variant show deeper in stack trace. throw ex; show test deepest level.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -