Results 1 to 10 of 52

Thread: Resume On Error GoTo 0 -1 GoTo Error Handling Statements Runtime VBA Err Handling ORNeRe GoRoT N0Nula 1

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #14
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10

    Part 2) Custom Error handler using Err object. Err.Raise.

    Err object. Err.Raise. Custom Error handler

    Part 2) Custom Error handler using Err object. Err.Raise.

    The conclusions from the last post are that this is just a complicated way to get a message box up to warn or inform of an error.
    The custom error handler, or rather the working part of it, .Raise is as un intuitive as most of the VBA error handling tools and concepts. One thing that does not follow obviously from the last post I that one thing that the Err.Raise effectively does is to replace any existing user defined error handling with the default VBA error handler, but with modified Properties as defined in the arguments of the .Raise:
    Err. Raise(Number:= , Source:= , Description:= , HelpFile:= , HelpContext:= , LastDllError:= )
    It appears to do this replacement even in the aroused exceptional state. So effectively we have pseudo
    _ [On Error GoTo -1 : Raise exception, use default VBA handler with these modified arguments of( 11 , , , , , ) ]

    As noted the use as in the last post of Err.Raise had little practical use. The fact that it appears to work in the Exception state would make it possible to use in such an example as below. There I use the On Error GoTo LabelOrLineNumber initially and then at the error handling code section sent to by the LabelOrLineNumber I will do the .Raise

    Consider the simple example looked at already a few times of an attempt to divide by zero.
    Based on the experiments from the last post I will decide to
    _ give an arbitrary, ( hopefully never used ), Number ,
    _ I will choose my own message ( .Description ) ,
    _ I will use the .Source always seen from the last post ( It appears to be possible to use anything at all here, - but just to be on the safe side I will use what appears the appropriate one )
    _ Use the appropriate help available for this sort of error

    The purpose of this code would be to punish the Twat that tried to divide by zero. A different error is handled more politely.
    Code:
    Sub CunstromErrorhandler()
     On Error GoTo ErrRaiseHandle
    ' An Error to be handled politely
    Dim Rng As Range
     Let Rng.Value = "AnyFink" ' Will error as my Rng has not been set so "doesn't exist"
    ' An Error to be punished
    Dim RslTwat As Double, Destrominator As Long
     Let RslTwat = 1 / 0
    '
    Exit Sub
    ErrRaiseHandle:
        If Err.Number = 11 Then
         Err.Raise Number:=42, Source:="VBAProject", Description:="You stupid Twat, you tried to divide by 0, as punishment I will end the code", HelpFile:="C:\PROGRA~1\COMMON~1\MICROS~1\VBA\VBA6\1031\VbLR6.chm", HelpContext:=1000011
        Else
         MsgBox "The error was " & Err.Description & vbCrLf & "The code will continue at the line just after the one which caused the error"
         Resume Next
        End If
    End Sub




    In the above code I can't see any major advantage of using the Err.Raise in place of a message box for a simple message, ( or if I wanted to use the Help then I could use the VBA Input box )
    If the error is not the divide by zero , then I use a more standard MsgBox using the Err Object Property information



    Here the same code again using the VBA Input box in place of the Err.Raise , that is to say doing effectively the same as far as the user is concerned. In this second code as we are not using Err.Raise at all, then we are not doing the likely pseudo _..
    _ [On Error GoTo -1 : Raise exception, use default VBA handler with these modified arguments of( , , , , , ) ]

    _.. to do later, or find it....
    Last edited by DocAElstein; 03-24-2023 at 09:31 PM.

Similar Threads

  1. Replies: 8
    Last Post: 09-01-2015, 01:50 AM
  2. Difference Between 'On Error GoTo 0' And 'On Error GoTo -1'
    By Transformer in forum Familiar with Commands and Formulas
    Replies: 7
    Last Post: 07-02-2015, 04:07 PM
  3. Replies: 2
    Last Post: 05-14-2013, 01:02 AM
  4. Runtime Error 481 invalid figure when PNG
    By Tony in forum Excel Help
    Replies: 0
    Last Post: 02-12-2013, 12:59 AM
  5. Replies: 10
    Last Post: 04-07-2012, 05:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •