2

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 ?

Rohit Saluja
  • 1,517
  • 2
  • 17
  • 25
  • Where are `sql` and `t` used? Why would an integer be a useful answer to the question "Is this recursive?" – jsheeran Apr 25 '16 at 11:24
  • You need a `Resume` statement of some kind (or `On Error Goto -1` if you enjoy spaghetti) to reset the current exception. – Rory Apr 25 '16 at 11:27
  • @jsheeran Well, I am not using these variables nor this code snippet is doing anything great. I have posted the question for learning purpose and out of curiosity as how to handle error in error handler – Rohit Saluja Apr 25 '16 at 11:27
  • @Rory Where should i enter Resume ? – Rohit Saluja Apr 25 '16 at 11:28
  • After you have dealt with the previous error to redirect flow back out of the error handler. – Rory Apr 25 '16 at 11:34
  • I added Resume after `t = InputBox("Is this recursive ?") ` but it is still giving me an error – Rohit Saluja Apr 25 '16 at 11:39

2 Answers2

1

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
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • What does On Error Goto -1 does ? – Rohit Saluja Apr 25 '16 at 11:41
  • @RohitSaluja: to find out more about `On Error Goto -1` or `Resume Next` just read the corresponding MSDN post: https://msdn.microsoft.com/en-us/library/5hsw66as.aspx – Ralph Apr 25 '16 at 11:43
  • 1
    IMO if you find yourself using OEG-1 you should really rethink your code. – Rory Apr 25 '16 at 11:45
  • @Rory, I agree. It is just elaborating and explaining his approach. – Paul Ogilvie Apr 25 '16 at 11:57
  • 1
    @PaulOgilvie Well to be honest, after reading through the above link. I still do not understand what is On Error GoTo -1 is ? – Rohit Saluja Apr 25 '16 at 12:06
  • 2
    It clears the current exception without changing the active error handler. Until you clear the exception, any further error will be unhandled. – Rory Apr 25 '16 at 12:18
0

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
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124