Determing What 'Level' A Subroutine Is?

jsurpless

Well-known member
Joined
Jul 29, 2008
Messages
144
Programming Experience
Beginner
Hi

It seems to me that there must be some way of using the stacktrace to determine what level a particular subroutine is... if I'm not using the right terminology, by this - I mean - an HMI click subroutine would be Level 1, a sub it calls Level 2, etc...

The reason I am asking is because I am working on my exception handling... if I generate a completely unexpected error, I want my code to shut down... so what I do is to throw a new user-defined exception ("UnexpectedException") that 'bumps' me up through my subroutines... I keep throwing this exception until I reach the top level but can't throw it on the top level as that would crash the program...

Hopefully this makes some sense?

Thanks!

-Justin
 
At the top level of the stack, write the error out to a DB or a log file or something then simply exit the app
 
Could you elaborate on how exactly I would go about this?

What I'm doing so far is to 'throw' a new exception as soon as I've determined I have an unexpected error... I then catch this UnexpectedException (custom) in each subroutine on the way up, throwing it again and again until I'm at the top...
 
And when you're at the top, you should log the error somewhere and exit the application.

For me to be any more specific I need to know how you're wanting to log the error.

As for exiting the app after the error's logged, one way to do it is to simply use Application.Exit() in the code, or if it's all on the main form and the app closes when the main form closes then a much better 'Me.Close()' would work well
 
Hi

I'm not looking to close the application but rather just to shut down the operation (i.e, the current procedure(s))... although, perhaps I should exit the application...

Here's what my error handling looks like... this is the Global, called from the Module level if it can't resolve the error...

VB.NET:
Public Sub Global_ErrorHandler(ByVal Error_MessageStr As String, ByVal Exception_Type As Exception)

        '-----

        Dim CurrentStack As New System.Diagnostics.StackTrace(True)
        Dim strCalling_Procedure As String = CurrentStack.GetFrame(2).GetMethod.Name

        Dim strActiveGlobalName As String = Exception_Type.TargetSite.DeclaringType.Name

        Dim Line_Number As String = Exception_Type.StackTrace.ToString.Substring(InStr(Exception_Type.StackTrace.ToString, ":line ") + 5)

        Dim strException_Type As String = Exception_Type.GetType.ToString
        Dim strException_Type_Message As String = Exception_Type.Message

        '-----

        Select Case strException_Type

            Case Else

                MsgBoxPrompt = "Module : " & strActiveGlobalName & vbCrLf & "Procedure : " & strCalling_Procedure & vbCrLf & "In Line #" & Line_Number & vbCrLf _
                    & vbCrLf & "Exception Type : " & strException_Type & vbCrLf & vbCrLf & strException_Type_Message _
                    & vbCrLf & vbCrLf & "****CONTACT ADG Developer****"
                MsgBoxTitle = "Unexpected Error" & " - " & Error_MessageStr

                MsgBoxReply = MsgBox(MsgBoxPrompt, vbExclamation, MsgBoxTitle)

                '-----

                strLogEntry = "------------------------------------------------------------------------" _
                & vbCrLf & UCase(MsgBoxTitle) _
                & vbCrLf & MsgBoxPrompt _
                & vbCrLf & "------------------------------------------------------------------------"

                Call Write_Log_Entry(strLogEntry)

                '-----

                'Throw UNEXPECTED Exception to TERMINATE CURRENT OPERATION
                Throw New UnexpectedException

        End Select

    End Sub

VB.NET:
    Public Sub Module_ErrorHandler(ByVal Error_MessageStr As String, ByVal Exception_Type As Exception)

        '-----

        Dim strException_Type As String = Exception_Type.GetType.ToString
        Dim strException_Type_Message As String = Exception_Type.Message

        '-----

        Select Case strException_Type

            Case Else

                'Pass the ERROR to the Global Error Handling Subroutine
                Call Global_ErrorHandler(Error_MessageStr, Exception_Type)

        End Select

    End Sub

Here's what the bottom of my subs and functions look like

VB.NET:
        Catch Exception_Type As UnexpectedException

            '-----

            'Throw UNEXPECTED Exception to TERMINATE CURRENT OPERATION
            Throw New UnexpectedException

        Catch Exception_Type As Exception

            Error_MessageStr = "Configuration.xml ReBuild FAILED"

            Call Module_ErrorHandler(Error_MessageStr, Exception_Type)

        End Try

So if my sub throw an unexpected error, it goes to the Global_ErrorHandler... which then throws an unexpected exception if it can't resolve... this gets bumped all the way back to the first run procedure (typically, HMI)
 
Back
Top