Positioning of an error trap
Positioning of an error trap
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: Positioning of an error trap

  1. #1
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sunny, spring-like Hull
    Posts
    3,339
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    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.

  2. #2
    Guest

    Default

    [quote]
    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.

    [quote]

    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

  3. #3
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    2,936
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default

    BTW, that was me - it's too early on a Sunday to be doing this

    Regards,
    Dan

  4. #4
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sunny, spring-like Hull
    Posts
    3,339
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    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 ]

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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

 

 
DMCA.com