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
Jatin NaharAsked:
Who is Participating?
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
You can change release mode settings but at end you will have same/similar out as of debug mode.

You can follow these steps for this purpose:
Go into the Properties window for the project where you want to see stack trace line numbers.
Go To Build tab.
Select Release configuration and check check the DEBUG constant parameter.
Uncheck the Optimize code parameter
Go To Advanced and choose Output ,  Debug Info , pdb-only.
xcopy new .pdb file deilverables.



Please refer to following urls for more detail:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28104240.html
http://stackoverflow.com/questions/628565/display-lines-number-in-stack-trace-for-net-assembly-in-release-mode
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,

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

Thanks
0
 
Fernando SotoRetiredCommented:
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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Fernando SotoRetiredCommented:
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.
0
 
käµfm³d 👽Commented:
It may not be exactly what you are looking for, but Caller Information Attributes may be of interest to you.
0
 
Jatin NaharAuthor Commented:
@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.
0
 
Fernando SotoRetiredCommented:
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

0
 
Jatin NaharAuthor Commented:
@Fernando Soto : Will this code provide the line number if i deploy the project, i mean after project goes live?
0
 
Fernando SotoRetiredCommented:
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.
0
 
Jatin NaharAuthor Commented:
@Fernando Soto : Any other way to get the line number?
0
 
Fernando SotoRetiredCommented:
Not that I am aware of.
0
 
käµfm³d 👽Commented:
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   ; )
0
 
Jatin NaharAuthor Commented:
@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
0
 
Jatin NaharAuthor Commented:
excellent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.