This article explains the exception handling scenario in .NET.
Objective
To handle errors in different scenarios.
Scenario 1: If the all the code lies in the same class there is no multilevel class functions.
Scenario 2: If the code lies in the different class there is multilevel class functions.
Using the code
**************************************************************************
Exception Handling Scenarios
**************************************************************************
Scenario 1: If the all the code lies in the same class there is no multilevel class functions.
//same code in class Program
//Program class
static int Test2()
{
int i = 0;
int j = 0;
try
{
j = i / i;
}
catch
{
throw ;
}
return j;
}
static void Main(string[] args)
{
try
{
Console.WriteLine(Program.Test2());
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
ERROR :
at ConsoleApplication2.Program.Test2() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 20
at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 30
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//same code in class Program
static int Test2()
{
int i = 0; int j = 0;
try {
j = i / i;
}
catch(Exception e)
{
throw e;
}
return j;
}
static void Main(string[] args)
{
try
{
Console.WriteLine(Program.Test2());
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
ERROR :
at ConsoleApplication2.Program.Test2() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 20
at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 30
Conclusion:
If the code lies in the same class there is no difference the way we put the try…catch block. The caller method (Main in this case) must have below code.
catch(Exception e)
{
Console.WriteLine(e.Message);
}
**************************************************************************
Scenario 2: If the code lies in the different class there is multilevel class functions.
//Program class
static void Main(string[] args)
{
try
{
Console.WriteLine(Class1.TestTest());
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
//another class which has two functions
class Class1
{
public static int TestTest()
{
try
{
return Class1.Test();// call the below function here
}
catch(Exception e)
{
throw e;
}
}
public static int Test()
{
int i = 0;
int j = 0;
try
{
j = i / i;
}
catch
{
throw;
}
return j;
}
}
at ConsoleApplication2.Class1.TestTest() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Class1.cs:line 18
at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 29
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
static void Main(string[] args)
{
try
{
Console.WriteLine(Class1.TestTest());
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read();
}
//another class
class Class1
{
public static int TestTest()
{
try
{
return Class1.Test();// call the below function here
}
catch
{
throw ;
}
}
public static int Test()
{
int i = 0;
int j = 0;
try
{
j = i / i;
}
catch
{
throw;
}
return j;
}
}
at ConsoleApplication2.Class1.Test() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Class1.cs:line 32
at ConsoleApplication2.Class1.TestTest() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Class1.cs:line 18
at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 29
Conclusion:
If the code lies in the multilevel classes there is a difference the way we put the try…catch block. The caller method (Main in this case) must have below code as usual.
catch(Exception e)
{
Console.WriteLine(e.Message);
}
If we put do not put catch(Exception e) in the lower levels functions. The Caller Function (Main Here) gives all the stack trace from where it is originated or else you will lose stack trace. The generated error propagates and bubble up to the caller function.
catch
{
throw;
}
Conclusion
If we put do not put catch(Exception e) in the lower levels functions. The Caller Function (Main Here) gives all the stack trace from where it is originated or else you will lose stack trace. The generated error propagates and bubble up to the caller function.