Getting Exact Location of Exception in C# Code

This article is about determining the exact location of an exception in code. There is always a problem for the developer to locate the exact location from where an exception is raised and because of that it is difficult for the developer to determine what actually went wrong.
 
Most of the time the problem occurs when there are too many libraries referenced, in other words used in the project.

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

  1. Font end Layer though which the user interacts with the application and displays data to the end user of the application.
  2. Business Layer havs the business logic, the code to call the Data Layer and the the info classes that transport between the front end and data layers.
  3. Data Layer that interacts with the database and supplies data to the 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 code abovem the front end layer calls the “GetEmployeeList” method to get the list of all employees.

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 calls the DataLayer to get the 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 the employee list, but for the example it returns an exception.

Now if you run the code above by putting it into the respective layer then when exception is thrown by the actual code debugger break execution in the front layer code. The following image shows that.



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

Solution

In Visual Studio there is the option, as shown in the following image, that allows the debugger to break execution when an exception occurs.



Once you click on the menu option it will open a dialog box where you can select “Common Language Runtime Exception” as in the following image and click "OK".


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



So by breaking at an exact location that an error occurs, you can determine which part is actually causing the problem and that helps to resolve the error easily.

Conclusion


The Visual Studio option to locate an exception is also very helpful when there are multiple threads doing an operation and throws an exception. Also the locate exceptions dialog provides other options that are also helpfull when you are using JavaScript, a COM component and so on that you can try by yourself.

Up Next
    Ebook Download
    View all
    Learn
    View all