Solved

MessageBox.Show(ex.StackTrace)

Posted on 2006-11-16
14
1,049 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 62

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 62

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 62

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 62

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 62

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
update textboxes with Streamreader 21 47
DataGridView Events ? 3 45
rebind a grid after user clicks on node in treeview 1 35
Complex SQL statement in VB.NET 7 14
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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