Solved

MessageBox.Show(ex.StackTrace)

Posted on 2006-11-16
14
1,059 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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