Multiple error handling

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I think neither methods below is ideal but of the two, which would be preferable?

Code:
Option Explicit

Sub FirstMethod()

    On Error GoTo FirstErrhandler
        ' Do something first
    On Error GoTo 0

    On Error GoTo SecondErrhandler
        ' Do something second
    On Error GoTo 0

    Dim a
    a = 2 ' Change the value of a to another number

    If a = 1 Then
     
        ' Do something third

    Else

       GoTo FirstErrhandler

SecondErrhandler:

        MsgBox Prompt:="Another message"
    
        Resume ResumeSecondErrhandler
    
FirstErrhandler:
                
        MsgBox Prompt:="Some message"
    
ResumeSecondErrhandler:
    
        ' Do something fourth

    End If

    On Error GoTo 0

End Sub




Sub SecondMethod()

    On Error GoTo FirstErrhandler

        ' Do something first

    On Error GoTo 0

    On Error GoTo SecondErrhandler

        ' Do something second

    On Error GoTo 0
    
    Dim a
    a = 1 ' Change the value of a to another number

    If a = 1 Then
     
        ' Do something third
    
    Else

FirstErrhandler:

        MsgBox Prompt:="Some message"

ResumeSecondErrhandler:
    
        ' Do something fourth

    End If

    On Error GoTo 0

    GoTo Exitpoint ' SHOULD BE Resume Exitpoint if there was an error but GoTo Exitpoint if there isn't

SecondErrhandler:

    MsgBox Prompt:="Another message"

    Resume ResumeSecondErrhandler

Exitpoint:

End Sub

I prefer the second but I can't accept this line, even though it works:

Code:
GoTo Exitpoint ' SHOULD BE Resume Exitpoint if there was an error but GoTo Exitpoint if there isn't, so how can I do both?
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Between your two option, I think your second is a little bit less a "spaghetti".

Concerning your GoTo Exitpoint, you could simply replace that by "Exit sub" which will quit the sub.

Normally, I do this to handle problem on my own code.

Code:
Function test()
On Error GoTo Problem

'Do something

Exit Function
Problem:
solarmultiplier = Err.Description
End Function
 
Upvote 0
Between your two option, I think your second is a little bit less a "spaghetti".

Concerning your GoTo Exitpoint, you could simply replace that by "Exit sub" which will quit the sub.

Normally, I do this to handle problem on my own code.

Code:
Function test()
On Error GoTo Problem

'Do something

Exit Function
Problem:
solarmultiplier = Err.Description
End Function

Thanks.

I used to use Exit Sub but was told it's clearer if subroutines only have one exit point, hence my GoTo Exitpoint..
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top