Solved

Exception Line Number

Posted on 2015-01-06
14
132 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
Comment Utility
Hi,

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

Thanks
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
@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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
@Fernando Soto : Will this code provide the line number if i deploy the project, i mean after project goes live?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
@Fernando Soto : Any other way to get the line number?
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not that I am aware of.
0
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 500 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
@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
Comment Utility
excellent
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now