Rohit Saluja
Rohit Saluja

Reputation: 1517

Error handling in Error handler

I am using below code, When user click on cancel button in the input box, the error is being handled by the error handler.

But if there is error again in the error handler then that error is not getting handled by the error handler.

Sub calculateroot()

    Dim msg As String, t as Integer
    On Error GoTo myhandle
    Dim inp As Integer, sql As Single
    inp = InputBox("Enter the number to find the square root")
    sql = Sqr(inp)
    Exit Sub
myhandle:
  t = InputBox("Is this recursive ?")
End Sub

What changes should I make in the code to handle the error generated in error handler ?

Upvotes: 2

Views: 2016

Answers (2)

Cees Timmerman
Cees Timmerman

Reputation: 19644

If you need to resume, this disgusting code works:

On Error Resume Next
parm = "bla"
DoSomething(parm)
If Err.Number > 0 Then
    Err.Clear
    parm = "oldbla"
    DoSomething(parm)
End If
If Err.Number > 0 Then
    Err.Clear
    parm = "evenolderbla"
    DoSomething(parm)
End If

Upvotes: 0

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

You have to reset the error handler and then set a new one:

Sub calculateroot()

    Dim msg As String, t As Integer
    On Error GoTo myhandle
    Dim inp As Integer, sql As Single
    inp = inputbox("Enter the number to find the square root")
    sql = Sqr(inp)
    Exit Sub
myhandle:
    On Error GoTo -1
    On Error GoTo myhandle2
    t = inputbox("Is this recursive ?")
     MsgBox t
    Exit Sub
myhandle2:
    MsgBox "myhandle2"
End Sub

Upvotes: 1

Related Questions