Solved

Exception Line Number

Posted on 2015-01-06
14
142 Views
Last Modified: 2015-01-08
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
0
Comment
Question by:Jatin Nahar
  • 5
  • 5
  • 2
  • +1
14 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 40533453
Hi,

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

Thanks
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40533478
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40533487
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40533515
It may not be exactly what you are looking for, but Caller Information Attributes may be of interest to you.
0
 

Author Comment

by:Jatin Nahar
ID: 40534931
@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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40534996
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
 

Author Comment

by:Jatin Nahar
ID: 40535280
@Fernando Soto : Will this code provide the line number if i deploy the project, i mean after project goes live?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40535442
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
 

Author Comment

by:Jatin Nahar
ID: 40535482
@Fernando Soto : Any other way to get the line number?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40535496
Not that I am aware of.
0
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 500 total points
ID: 40535571
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40535746
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
 

Author Comment

by:Jatin Nahar
ID: 40537219
@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
 

Author Closing Comment

by:Jatin Nahar
ID: 40539528
excellent
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question