• Word Error Handling (Word 97 SR2)

    Home » Forums » Developers, developers, developers » DevOps Lounge » Word Error Handling (Word 97 SR2)

    Author
    Topic
    #381301

    And with the “try catch” in .NET, it is finally possible to apply an error handler which can be used in all routines without having to modify forhard-coded routine names.

    But they still missed out (I think) in being able to apply a single error handler over, say, an entire form, without having to code indiviually into each sub or function (I used to be able to do this im mainframe- what happened in the name of progress?). This would be overridable of course.

    Viewing 2 reply threads
    Author
    Replies
    • #642145

      >> apply an error handler which can be used in all routines without having to modify forhard-coded routine names.

      Geoff,

      Could you ‘splain that for me? Ah ain’t clear on yur meanin’

      • #642221

        In VB6, I put in an error handler for every module. I have to change that error handler for each module to include the module name. Neither VB6 nor VBA can give me the module name without having to hardcode something.

        (eg:

        Sub ShowExample()
            On Error GoTo ErrorHandler
        ...
            Exit Sub
        
        ErrorHandler:
            Msgbox "Err " & Err.Number & " occurred in routine ShowExample"
        

        But in .NET I don’t have to hardcode “ShowExample”- I can determine the routine name programatically (I don’t have .NET here, I can’t rememeber just how off the top of my head)

        • #642224

          Gee wiz. That’s great. Mr. Liberty never mentioned that in the CBT I’ve just
          finished. Remeber the posts a while back where I tried to figure out a
          way to get that mod name. This is the best I could do. It’s pretty lame,
          but if the actual error message is unique, it works.

          Function GetSubName(ByVal UniqueStr As String, ByVal strMod As String) As String
          Dim Line As Long
          Dim oMod As Object
          Line = 1
          Set oMod = VBE.ActiveVBProject.VBComponents(strMod).CodeModule
          With oMod
          If .Find(UniqueStr, Line, 1, .CountOfLines, 1) Then
          GetSubName = .ProcOfLine(Line, 0) ‘ 0 = vbext_pk_Proc
          End If
          End With
          Set oMod = Nothing
          End Function

          • #642229

            I remember that. From memory though, it did not work in compiled VB.

            I’ve looked up a manual. I think the info is in the System.Exception class- Source property (and even the StackTrace property)- http://support.microsoft.com/default.aspx?…-us;301283&%5B/url%5D

            I wonder if it’s possible to wrap a “try catch” around all the code in a module to save having to code modules individually

            • #642235

              Looks like the Source property only gives the project or application name. I’d like to know
              what class raised the exception, the procedure name, the line number and maybe the user’s shoe size as well.

              I guess you can parse all that (except the shoe size) from the StackTrace, but that ain’t pretty.

            • #642239

              It looks as if the StackTrace can be fairly easy to use- see http://msdn.microsoft.com/msdnnews/2001/ma…m/Classroom.asp%5B/url%5D.

              I can’t try it for a while though.

            • #642253

              Careful, the namespace for the StackTrace class in C# is

              System.Diagnostics

              not System.Reflection

              weird that they’d be different. Oh well.

    • #641892

      (Edited by gwhitfield on 04-Jan-03 09:54. Moved from the VB/VBA board)

      I’ve never used it myself. I’ll probably never use it now that I have

      try
      catch
      finally

      finally!!

    • #643027

      You can set an error handler for an entire form in .NET like this.

      Sub ShowMyForm()
      dim myForm as New whateverYourFormNameIs

      Try
      myForm.Show or ShowDialog
      Catch
      your error handlingcode
      End Try

      This should, according to the documentation, work just fine.

      Regards,

      Kevin Bell

      • #643349

        In fact, using the Sub Main() method of initiating a program in .NET, you can set a single error handler for the whole program.

        Sub Main()
        Dim myForm as New StartingFormName

        Try
        MyForm.Show
        Catch
        Do the code for the error handling
        Finally
        Whatever yoy want here
        End Try

        I haven’t used this in anger but does work.

        Regards,

        Kevin Bell

      • #644591

        Thanks Kevin.

        I’ve found another way to do the same:

        Private Sub Form1_Load(ByVal sender As Object, _
               ByVal e As System.EventArgs) Handles MyBase.Load 
            AddHandler Application.ThreadException, _
                New ThreadExceptionEventHandler(AddressOf Me.CatchAllExceptions)
        End Sub
        '
        Public Sub CatchAllExceptions(ByVal Sender As Object, _
               ByVal Excep As ThreadExceptionEventArgs) 
            MsgBox("Unhandled Exception:" & Excep.Exception.Message)
        End Sub
        '
        Private Sub Button1_Click(ByVal sender As System.Object, _
             ByVal e As System.EventArgs) Handles Button1.Click  
        Dim BlowMeUp As Integer
            BlowMeUp = "a"
        End Sub
    Viewing 2 reply threads
    Reply To: Word Error Handling (Word 97 SR2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: