Link to home
Start Free TrialLog in
Avatar of Jatin Nahar
Jatin Nahar

asked on

Exception Line Number

Hi Experts,

I am working on the application using visual studio 2013 winforms. I want to fetch the line number of exception in catch block. How can i achieve this?

Please help me to solve this problem.

Thanks in advance

Regards
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Hi,

you can get  line numbers in exception through stack traces. But It seems to work in debug mode only.

Thanks
Use the exception variable in the catch clause to get the info for example, ex.StackTrace, which returns a string like the following.

" WindowsFormsApplication4.Form1.button1_Click(Object sender, EventArgs e) in c:\Working Directory\MSDN Forum\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs:line 31"

This part "WindowsFormsApplication4.Form1.button1_Click(Object sender, EventArgs e) " is the method call it happened in.
This part "c:\Working Directory\MSDN Forum\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs" is the class it happened in.
The line number it happened in :line 31
As @Imran Javed Zia, has stated this info is only in Debug release because this information is in the pdb file which is not available during Release mode of the application.
It may not be exactly what you are looking for, but Caller Information Attributes may be of interest to you.
Avatar of Jatin Nahar
Jatin Nahar

ASKER

@Fernando Soto: Thank you for your response. Actually i want to create the error log file and i have to insert the line number which is causing the error. How can i achieve this? please help me.
Hi Jatin;

Given the following code snippet, the catch block shows where to get the stack trace info from.

try
{
    string s = null;
    ProcessString(s);
}
catch (Exception ex)
{
    Console.WriteLine("{0} Exception caught.", e);
    // This console message prints the information I explained in my last post
    // Get the line number from the end of the string.
    Console.WriteLine("{0}", ex.StackTrace);
}

Open in new window

@Fernando Soto : Will this code provide the line number if i deploy the project, i mean after project goes live?
From Microsoft Documentation.
StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects.

So in answer to your question, No.
@Fernando Soto : Any other way to get the line number?
Not that I am aware of.
ASKER CERTIFIED SOLUTION
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is why it's important to not have bloated methods:  the more code that exists in a method, the more ambiguity can come in determining where the error occurred. Smaller methods make for less code to inspect in error scenarios. Now's as good a time as any to work on your refactoring skills   ; )
@Imran Javed Zia:

Thank you for your response. I have followed your instructions but still not getting the line number where exception is occuring. Please see the attached snap shot and let me know your feedback.
1.png
excellent