Solved

MessageBox.Show(ex.StackTrace)

Posted on 2006-11-16
14
1,043 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
  • 5
  • 4
  • 2
  • +1
14 Comments
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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
Comment Utility
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
Comment Utility
What would be an example of how to call that.  Currently all I have is the stacktrace information.
0
 

Author Comment

by:flfmmqp
Comment Utility
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 69

Expert Comment

by:Éric Moreau
Comment Utility
Hi flfmmqp,

> IsNumeric is getting an error

try microsoft.visualbasic.isnumeric

Cheers!
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 62

Expert Comment

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

Author Comment

by:flfmmqp
Comment Utility
Will getting the file name work in release mode?
0
 
LVL 62

Expert Comment

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

Expert Comment

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

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

18 Experts available now in Live!

Get 1:1 Help Now