Solved

MessageBox.Show(ex.StackTrace)

Posted on 2006-11-16
14
1,055 Views
Last Modified: 2012-06-27
I want to trim the Stack Trace on the exceptions Stack trace to just give me the module or form and the line number the at the code broke on.  How can I do this?  Looking for code please.  Also can you pull the error number in VB.net 2003?

example code:

    Sub Main()
        Try
            Dim file As New FileStream("c:\test\test.txt", FileMode.Open)
            Dim reader As New StreamReader(file)
            Dim line As String = reader.ReadLine()

            MessageBox.Show(line)
            reader.Close()
            file.Close()
        Catch ex As Exception
            MessageBox.Show(ex.StackTrace)
        End Try

    End Sub
0
Comment
Question by:flfmmqp
[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
  • 4
  • 2
  • +1
14 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 17959296
try something like that:

Sub GetFileAndLineNo(ByVal str As String, ByRef file As String, ByRef lineno As Integer)
        Dim strTemp, arrStack(), arrTemp() As String
        arrStack = str.Split(" ")
        file = arrStack(arrStack.Length - 2)
        If IsNumeric(arrStack(arrStack.Length - 1)) Then
            lineno = arrStack(arrStack.Length - 1)
        End If
    End Sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17959311
call it like so

Sub Main()
        Try
            Dim file As New FileStream("c:\test\test.txt", FileMode.Open)
            Dim reader As New StreamReader(file)
            Dim line As String = reader.ReadLine()

            MessageBox.Show(line)
            reader.Close()
            file.Close()
        Catch ex As Exception

            MessageBox.Show(ex.StackTrace)
Dim filename As String
            Dim lineno As Integer
            GetFileAndLineNo(ex.StackTrace, filename, lineno)
            if lineno <> 0 then
            MessageBox.Show("file is " & filename & " and line is " & lineno)
           end if
        End Try

    End Sub

Sub GetFileAndLineNo(ByVal str As String, ByRef file As String, ByRef lineno As Integer)
        Dim strTemp, arrStack(), arrTemp() As String
        arrStack = str.Split(" ")
        file = arrStack(arrStack.Length - 2)
        If IsNumeric(arrStack(arrStack.Length - 1)) Then
            lineno = arrStack(arrStack.Length - 1)
        Else
            lineno=0
        End If
    End Sub
0
 

Author Comment

by:flfmmqp
ID: 17959335
What would be an example of how to call that.  Currently all I have is the stacktrace information.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:flfmmqp
ID: 17959368
impressive that you answered my question before I asked it.  IsNumeric is getting an error.  I have this in a class and would like to keep it there.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 17959394
Hi flfmmqp,

> IsNumeric is getting an error

try microsoft.visualbasic.isnumeric

Cheers!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17959408
Hi flfmmqp;

This will give the method in the code the error happened in.

example code:

    Sub Main()

        Try
            Dim file As New FileStream("c:\test\test.txt", FileMode.Open)
            Dim reader As New StreamReader(file)
            Dim line As String = reader.ReadLine()

            MessageBox.Show(line)
            reader.Close()
            file.Close()
        Catch ex As Exception
            Dim st As New StackTrace
            Dim currentFrame As StackFrame = st.GetFrame(0)
            Dim methodName As String = currentFrame.GetMethod.Name

            MessageBox.Show("Error occured In method " & methodName)

        End Try

    End Sub


Fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17959503
Be aware that line numbers only work in debug mode and will not be reported in Release mode.
0
 

Author Comment

by:flfmmqp
ID: 17959753
Will getting the file name work in release mode?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17959903
Hi flfmmqp;

To your question, "Will getting the file name work in release mode?", Yes it will. I modified the code to also get the executing file name that the code is in. This code uses the meta-data of the compiles .Net exe. Getting the line number requires that the .pdb debug file is being used when the code is being executed.

            Dim st As New StackTrace
            Dim currentFrame As StackFrame = st.GetFrame(0)
            Dim moduleName As String = currentFrame.GetMethod.Module.Name    ' Add this line to get file name as well
            Dim methodName As String = currentFrame.GetMethod.Name

            ' Modify this line because of the added info above
            MessageBox.Show("Error occured in class " & moduleName & " in method " & methodName)


Fernando
0
 

Author Comment

by:flfmmqp
ID: 17964951
I am getting an error on this line:
           Dim moduleName As String = currentFrame.GetMethod.Module.Name    ' Add this line to get file name as well

Error:  'Module' is not a member of 'System.Reflection.MethodBase'.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17966663
Sorry flfmmqp;

The method Module.Name is only supported in VS 2005. I will look into how that can be done in VS 2003.

Fernando
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17972210
Hi flfmmqp;

This will work in VS .Net 2003

            Dim st As New StackTrace
            Dim currentFrame As StackFrame = st.GetFrame(0)
            ' Get the method name in which the error occured in
            Dim MethodName As String = currentFrame.GetMethod.Name
            ' Gets the Class or module in which the error occured in
            Dim ClassName As String = currentFrame.GetMethod.ReflectedType.Name
            ' Gets the full assembly name where the error occured in.
            Dim AssemblyName As String = currentFrame.GetMethod.ReflectedType.Assembly.FullName

            MessageBox.Show(String.Format("Error occured in Assembly {0} Class: {1} method: {2}", _
                AssemblyName, ClassName, MethodName))

Fernando
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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