Return error variable

Suriv

Active member
Joined
Jul 16, 2007
Messages
33
Programming Experience
1-3
Is there any way to display the variable where an Unhandled Exception occurred in a compiled application?

e.g I let the application crash with this code
VB.NET:
Dim CrashString(2) As String
        For c As Integer = 0 To 3
            CrashString(c) = "CrashValue"
        Next
So I want to display the name "CrashString()".

Or maybe there's a way to display some more information about a crash than just the StackTrace, Message and InnerException?

Thanks in advance :).
 
You should be able to write some code in the catch block and parse the StackTrace to get the name of the last method call. Something like this :

VB.NET:
Try
            methodName(2, 4)
        Catch ex As Exception
            Dim signature As New System.Text.StringBuilder(ex.TargetSite.Name + "(")

            If ex.TargetSite.GetParameters().Length > 0 Then
                For Each argument As System.Reflection.ParameterInfo In ex.TargetSite.GetParameters()
                    signature.Append(argument)
                    signature.Append(", ")
                Next

                signature.Remove(signature.Length - 2, 2)
            End If

            signature.Append(")")

            MessageBox.Show(signature.ToString())
        End Try

and here's the method :

VB.NET:
    Private Sub methodName(ByVal x As Integer, ByVal y As Integer)
        Throw New Exception()
    End Sub
 
That does work, but I mean to return a variable, not the method.

I want to show this variable (or more information about the crash) in ApplicationEvents.vb: MyApplication_UnhandledException.

Any more ideas? :)
 
I am not certain what you mean there... Do you want to show the values of the parameters? In which case I do not think it is possible. However, there is some more information about the method that threw the exception in ex.TargetSite.

As for using this in the MyApplication_UnhandledException event handler, all you need to do is use the event argument to get the exception that was thrown (I haven't tried it, but it certainly is available). That way, you can write something like the code I provided to get the signature and some more information about the method that initially threw the exception.
 
I want to see which variable caused the error e.g:
VB.NET:
Dim CrashString(2) As String
        For c As Integer = 0 To 3
            CrashString(c) = "CrashValue"
        Next
The variable I want to see is "CrashValue". I need this because my app uses a lot of variables, and if something goes wrong with one of those variables, I need to know which variable.

Thx :).
 
I am pretty certain that is not possible unless you keep the values in an instance variable somewhere and refer to the object that threw the exception to retrieve the value... Once the exception broke you ou of the current method, the local variables you were using got out of scope and were garbage collected (garbage collection is not immediate, but you get the general idea), so there is no functionnal way of retrieving a local variable's value.

Now, what you could do... You could use a static variable, or even use the settings (if you save he settings in case of a crash, you may retrieve the last operation on statup and say your app crashed if it's not set to "closing app"). That way, simply add a setting and set it's value to the name of the current operation whenever you need to.
 
And is there any other way to help me get more information about which variable has caused an unhandled exception?
 
same problme with you

hi, I got same problem with you, I need to get the intance of parameters during the expection. like if the input parameter is a instance of Class User, then I need to get the user, include user.name, user.email.... sth like that. if you can handle this problem, please let me know. Thank you so much
 
I did not work with them myself (altough I should :eek:) but .NET has logging possibilities that could allow you to track every action of your program and have very descriptive logs that describe errors that happened in your app.
 

Latest posts

Back
Top