tags:

views:

103

answers:

4

Hi,

in a catch block, how can I get the line number which thrown an exception?

+1  A: 

You could include .PDB symbol files associated to the assembly which contain metadata information and when an exception is thrown it will contain full information in the stacktrace of where this exception originated. It will contain line numbers of each method in the stack.

Darin Dimitrov
A: 

Simple way, use the Exception.ToString() function, it will return the line after the exception description.

You can also check the program debug database as it contains debug info/logs about the whole application.

+3  A: 

If you need the line number for more than just the formatted stack trace you get from Exception.StackTrace, you can use the StackTrace class:

try
{
    throw new Exception();
}
catch (Exception ex)
{
    // Get stack trace for the exception with source file information
    var st = new StackTrace(ex, true);
    // Get the top stack frame
    var frame = st.GetFrame(0);
    // Get the line number from the stack frame
    var line = frame.GetFileLineNumber();
}

Note that this will only work if there is a pdb file available for the assembly.

Quartermeister
A: 

In Global.resx file there is an event called Application_Error

it fires whenever an error occurs,,you can easily get any information about the error,and send it to a bug tracking e-mail.

Also i think all u need to do is to compile the global.resx and add its dll's (2 dlls) to your bin folder and it will work!

Khaled