Positioning of an error trap

Mudface

MrExcel MVP
Joined
Feb 18, 2002
Messages
3,339
I have a fairly lengthy routine which seems to be fairly tight, but unfortunately will be used by our QC dept, who can break almost anything :). The main part of the routine looks similar to this: -

Public Sub Main()

Call Sub1
Call Sub2
Call Sub3
call Sub4

End Sub

If I wrap an error trap around the above, will any errors thrown up in the called subs be caught by it? Or do I need a separate trap within each sub?

Thanks for any help.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I have a fairly lengthy routine which seems to be fairly tight, but unfortunately will be used by our QC dept, who can break almost anything . The main part of the routine looks similar to this: -

Public Sub Main()

Call Sub1
Call Sub2
Call Sub3
call Sub4

End Sub

If I wrap an error trap around the above, will any errors thrown up in the called subs be caught by it? Or do I need a separate trap within each sub?

Thanks for any help.

Hi Mudface,

The answer to your question is yes. If Sub Main has an error handling statement and an error occurs in Sub1 the error will be handled in Sub Main. However, if you have a statement in Sub1 which handles errors then that will take priority over the one in Sub Main. Another point worth bearing in mind is that if in Sub1 you have a line like On Error Goto ErrSub1 but then later you use On Error Goto 0 error handling will be return to Sub Main or whatever sub called Sub1.

If you set a breakpoint on Proc2 and step through this code you'll see what I mean.

HTH,
Dan.

Code:
Sub Main()
On Error GoTo errHandler

Proc2

Exit Sub
errHandler:
MsgBox "Error " & Err.Description

End Sub


Sub Proc2()
On Error GoTo errhndle
Dim x As Long

x = 1 / 0

On Error GoTo 0

x = 1 / 0

errhndle:

MsgBox "Error in proc2"

Resume Next
End Sub
 
Upvote 0
BTW, that was me - it's too early on a Sunday to be doing this :)

Regards,
Dan
 
Upvote 0
Thanks Dan, that'll save a fair bit of typing, I just wanted something simple to show there had been an error and that I should be notified, rather than having the user dumped out unceremoniously.

I was going to try and raise errors within each of the subs and see what happened, but forgot to bring the file home with me :).

Early?! Not when you've been up since 4 with a 4 month old with a cold... :(
This message was edited by Mudface on 2002-03-10 05:51
 
Upvote 0

Forum statistics

Threads
1,213,492
Messages
6,113,967
Members
448,537
Latest member
Et_Cetera

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