?
Solved

Exception Line Number

Posted on 2015-01-06
14
Medium Priority
?
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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