Click here to Skip to main content
13,292,670 members (65,684 online)
Click here to Skip to main content
Add your own
alternative version

Stats

22K views
11 bookmarked
Posted 28 Jan 2014

Getting Exact Location of Exception in C# Code

, 2 Feb 2014
Rate this:
Please Sign up or sign in to vote.
How to get exact location of exception in C# code

Introduction

This post is about locating the exact location of the Exception in the code. There is always a problem for the developer to locate the exact location from where Exception was raised, because of that, it is difficult for the developer what actually went wrong. Most of the time, problem occurs when there are too many libraries referenced, i.e., used in project.

To understand, let’s consider the below example: In the following example, the application is divided in three layers.

  1. Font end - Layer though which user interacts with application and displays data to end user of application.
  2. Business - Layer which has the business logic, code to call datalayer and the info classes which transport between font and data layer.
  3. Data - Layer which interacts with database and supplies data to business layer.

Front Layer Code

class Program
    {
        //Program p = new Program();

        public int i = 10;
        public static void Main(string[] args)
        {
            try
            {
                (new BusinessEmployee()).GetEmployeeList();
            }
            catch (Exception ex)
            {
                global::System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
    }

As you see in the above code Font layer is calling “GetEmployeeList” method to get list of all employee.

BusinessLayer Code

public class BusinessEmployee
{
    private readonly DataEmployee dataEmployee;

    public BusinessEmployee()
    {
        dataEmployee = new DataEmployee();
    }

    //Business layer class for employee
    public  void GetEmployeeList()
    {
        dataEmployee.GetEmployeeList();
    }
}

In this layer, the Business class is calling DataLayer to get employee list from the database.

DataLayer Code

public class DataEmployee
{
    //Data layer class for employee
    public  void GetEmployeeList()
    {
        throw new Exception("Not able to fetch employee");
    }
}

This layer returns employee list, but for the example, it's returning exception.

Now if you run the above code by putting it into the respected layer, then when exception is thrown by the actual code, debugger breaks execution in the front layer code. The below image shows the same thing.

So it becomes difficult for the developer what went wrong if there is actual complex code.

Solution

In Visual Studio, there is an option as given in the below image that allows to break debugging at the point, i.e., at the code from where exception is coming.

Once you clicked on menu option, it will open up dialog box where you can mark tick on “Common Language Runtime Exception” as in the below image and click ok.

Now if you run the same code, then the debugger will stop at the exact location from where exception is coming, you can also see the below image.

So breaking at exact location from where error is coming, you can find out which part is actually causing the problem and help to resolve the error easily.

Conclusion

Visual Studio option to locate exception is also very helpful when there are multiple threads doing operation and throws exception. Also locate exceptions dialog provides other option that are also helpful when you are using JavaScript, com component, etc. that you can try by yourself.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 4 Pin
Pratik Bhuva9-Feb-14 23:03
professionalPratik Bhuva9-Feb-14 23:03 
GeneralRe: My vote of 4 Pin
Pranay Rana10-Feb-14 0:18
memberPranay Rana10-Feb-14 0:18 
GeneralMy vote of 5 Pin
JCahyaatnttearjee4-Feb-14 2:38
memberJCahyaatnttearjee4-Feb-14 2:38 
GeneralRe: My vote of 5 Pin
Pranay Rana4-Feb-14 2:44
memberPranay Rana4-Feb-14 2:44 
GeneralRe: My vote of 5 Pin
JCahyaatnttearjee4-Feb-14 2:46
memberJCahyaatnttearjee4-Feb-14 2:46 
Now I am reading
Quote:
Dependency Injection For Beginner - 1
... Smile | :)
GeneralMy vote of 5 Pin
Mike Meinz29-Jan-14 8:03
memberMike Meinz29-Jan-14 8:03 
GeneralRe: My vote of 5 Pin
Pranay Rana30-Jan-14 1:52
memberPranay Rana30-Jan-14 1:52 
GeneralMy vote of 4 Pin
Klaus Luedenscheidt28-Jan-14 20:02
memberKlaus Luedenscheidt28-Jan-14 20:02 
GeneralRe: My vote of 4 Pin
Pranay Rana28-Jan-14 23:37
memberPranay Rana28-Jan-14 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 3 Feb 2014
Article Copyright 2014 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid