Getting detailed information about exceptions using C#

OK, now we know about exception handling and also how to use the try-catch-finally blocks to handle exception in C# code. The next question is how to get information about what has caused the exception. Only if we can know the cause of the exception we can ask the programmers to fix it. To answer this question we will have to examine the catch block a little more in details.

Arguments to the catch block

In the previous tutorials you had used the catch block without arguments. Although it is not wrong to do so but it does not help you much in knowing what has happened. Therefore it is very important to pass arguments to the catch block so that the actual cause of the exception can be known along with detailed information on what caused the exception.

The Exception class

The default (and general) arguments which you can pass to the catch block are of the type Exception. This is how your try-catch block should look by default

try  {      // Code here  }  catch (Exception ex)  {      // Exception handling code here.  }

See how we are passing argument ex of the type Exception into the catch block. When some exception will occur you will see the detailed information about the error in the ex variable.

Message and StackTrace

The two important properties of the Exception class you should be interested in are the Message and StackTrace. The message property tells you about the actual exception. The StackTrace on the other hand will give you the detailed information on the code which caused the exception along with the line number. Ideally you should be able to know each and everything about the exception from the StackTrace. Message is just a short summary on what happened; StackTrace is the complete story behind the exception.


Look at the code below

static void Main(string[] args)  {      try      {          Console.Write("Enter a number : ");          int i = Convert.ToInt16(Console.ReadLine());            // If input is correct then print this line          Console.WriteLine("
Correct input received...");      }      catch(Exception ex)      {          // If input is wrong then print this line          Console.WriteLine("
Inside the catch block");          Console.WriteLine("
Message : "+ ex.Message);          Console.WriteLine("
Full stack trace : 
");          Console.WriteLine(ex.StackTrace);      }      finally      {          // This code will always run          Console.WriteLine("
** Finally block executed (always) ");      }  }

Here inside the catch block we are printing the exception message and the complete stack trace to know the details of the exception. Below you can see the output of this program when we enter a wrong input

Exception handling in C#
Here you can see that the code went inside the catch block. The exception message is just one line but the stack trace is quite elaborate. You might not be able to understand the StackTrace right now but what you should keep in mind is that it gives you the full story behind the exception. Then in the end you can see that the code inside the finally block got executed.

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp