Jatin Nahar
asked on
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
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
Use the exception variable in the catch clause to get the info for example, ex.StackTrace, which returns a string like the following.
" WindowsFormsApplication4.F orm1.butto n1_Click(O bject sender, EventArgs e) in c:\Working Directory\MSDN Forum\WindowsFormsApplicat ion4\Windo wsFormsApp lication4\ Form1.cs:l ine 31"
This part "WindowsFormsApplication4. Form1.butt on1_Click( Object sender, EventArgs e) " is the method call it happened in.
This part "c:\Working Directory\MSDN Forum\WindowsFormsApplicat ion4\Windo wsFormsApp lication4\ Form1.cs" is the class it happened in.
The line number it happened in :line 31
" WindowsFormsApplication4.F
This part "WindowsFormsApplication4.
This part "c:\Working Directory\MSDN Forum\WindowsFormsApplicat
The line number it happened in :line 31
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.
It may not be exactly what you are looking for, but Caller Information Attributes may be of interest to you.
ASKER
@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.
Hi Jatin;
Given the following code snippet, the catch block shows where to get the stack trace info from.
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);
}
ASKER
@Fernando Soto : Will this code provide the line number if i deploy the project, i mean after project goes live?
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.
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.
ASKER
@Fernando Soto : Any other way to get the line number?
Not that I am aware of.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ; )
ASKER
@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
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
ASKER
excellent
you can get line numbers in exception through stack traces. But It seems to work in debug mode only.
Thanks