Exception : {Throw} vs {Throw ex}
I have seen two way how people typically throw exception:
Example 1:
try
{
//...
}
catch(Exception ex)
{
throw;
}
Example 2:
try
{
//...
}
catch(Exception ex)
{
throw ex;
}
While both accomplish the objective, which is to throw the exception back to the
caller for further action, it has substle difference.
In the first example, it is considered you are re-throwing the existing exception.
The exception contain the full stack frame from where the exception is orginated
all the way up to the caller stack frame.
Run the sample code below, and you shall see the stack trace as below. Notice, at each stack frame, the stack trace contain the stack frame of where the exception is orginated all the way up to the caller stack frame.
In the second example, it is considered you are throwing a new exception.
The exception only contain stack frame of the caller and the frame which throw
the exception. Run the sample code again, but this time change the throw to throw ex. This time, you will notice at each stack frame, the stack trace only contain the stack frame of the callee and the caller. So, you lose information of where the exception is originated. This can make locating the source of error very difficult.
Sample Code
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
Console.WriteLine(f1());
}
catch(Exception ex)
{
Console.WriteLine("---Exception in Main()---");
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace.ToString());
}
Console.ReadLine();
}
static int f1()
{
try
{
return f2();
}
catch(Exception ex)
{
Console.WriteLine("---Exception in f1()---");
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace.ToString());
throw;
}
}
static int f2()
{
try
{
return f3();
}
catch(Exception ex)
{
Console.WriteLine("---Exception in f2()---");
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace.ToString());
throw;
}
}
static int f3()
{
int a = 0;
int b = 1;
try
{
return b /a;
}
catch(Exception ex)
{
Console.WriteLine("---Exception in f3()---");
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace.ToString());
throw;
}
}
}
Labels: DotNet
0 Comments:
Post a Comment
<< Home