Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MessageBox.Show(ex.StackTrace)

Posted on 2006-11-16
14
Medium Priority
?
1,068 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
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.

 

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 64

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 64

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 64

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 64

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

916 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