Visual studio and .NET tip 8:- Get inner most exception

By Shivprasad Koirala

We all know exceptions get propagated from the last caller to the main caller. For instance let’s say from your static void main method you are calling “SomeMethod” and he in turn is calling “SomeMethod1”.

Visual studio and .NET tip 8:- Get inner most exception

We all know exceptions get propagated from the last caller to the main caller.  For instance let’s say from your static void main method you are calling “SomeMethod” and he in turn is calling “SomeMethod1”.
Now let’s say this “SomeMethod1” unfortunately raises error, which is sent to “SomeMethod” and he propagates the same to your main function.



Below is how the code snippet looks like.

static void Main(string[] args)
{

             try
            {
                 SomeMethod();
             }
             catch (Exception ex)
            {
               Console.WriteLine(ex.GetBaseException().Message.ToString());
            }
          
        }

         public static void SomeMethod()
        {
             try
            {
                 SomeMethod1();
             }
             catch (Exception e)
             {
                 throw new Exception("Somemethod",e); ;
            }
        }

         public static void SomeMethod1()
        {
             throw new Exception("Error thrown by somemethod1");
        }


Now if you try to catch the exception in static void main you would get exception message which is raised from “SomeMethod” method as shown in the below figure. As a curios developer you would always like to know the main source of error and not the disguised method.




Thanks to .NET there is nice function in the exception class called as “GetBaseException” as shown in the below code snippet.

Console.WriteLine(ex.GetBaseException().Message.ToString());


Now if there is a error raised by “Somemethod1” you should see the same in your main function as shown in the below image.




You can also see the video for the above tip and trick at  http://youtu.be/j3sQ65GB7OI?hd=1

Related FAQs

Many times as a developer you come across functions with lots of input parameters as shown in the below code snippets. In real projects the input parameters would be much higher as compared to the below code snippets. Some times for various reasons you want to shuffle them, reorder them or remove some of them.
Many times due to project pressure and lazy attitude you violate encapsulation and create public variables for classes as shown below. Even though your inner heart knows that the best practice is to create set and get property function but your lazy attitude overrules it.
While debugging you often want to skip debugging on certain lines of code. For instance in the below code you have set the debug point to the first line, you would like to skip the in between lines and jump directly to “console.writeline” step.
In big project you have 100’s of classes and each of those classes can have lots of properties.
As developer debugging is your routine job and you would like your debugger to debug smartly rather than monotonously. For instance in the below code we have put a debug point and we do not want our debugger to just break monotonously.
Now many times as a .NET developer we need connection strings to connect to databases. Connection string are long and cryptic and very difficult to remember. In this tip we will see a easy way of getting connection strings.
Visual studio and .NET tip 8:- Get inner most exception  (2083 Views)