Solved

Exception Line Number

Posted on 2015-01-06
14
146 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

713 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