Results 1 to 4 of 4

Thread: [2005] Stopping current execution

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    [2005] Stopping current execution

    Sorry if this is a basic question but can I terminate the actions of parent subs from within a lower sub?

    For example;

    VB Code:
    1. Private Sub MainSub
    2.   For i = 1 To 500
    3.     TopSub()
    4.   Next
    5. End Sub
    6.  
    7. Private Sub TopSub()
    8.     For i = 1 to 1000
    9.     'Call the lower sub
    10.     LowerSub()
    11.   Next
    12. End Sub
    13.  
    14. Private Sub LowerSub()
    15.   Try
    16.     'Do stuff
    17.   Catch
    18.     'I want to be able to terminate any further execution of "Main"
    19.     'from here if things go badly wrong (but leave the form up)
    20.   End Try
    21. End Sub
    So if the user clicks a button which calls the MainSub and something nasty happens, I dont want the execution to continue through all remaining loops, but I also dont want to close the application.

    Since some subs are functions, it makes it hard to pass an "OK" var up from each and every sub to its parent. I'm hoping there's a nifty command like Application.StopExecution(MainSub) or similar.

    I have this issue because I'm cycling through file accesses over the internet and if I get an authentication error or a timeout, I want to offer the user the opportunity to abort any further processing and let them fix the problem.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Stopping current execution

    Catch the exception at a higher level. You can either not catch the exception in LowerSub or else assign it to the InnerException of a new exception and throw that. That exception will then propagate back up the call stack and you can catch it wherever you need notification to occur.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Stopping current execution

    ok, I think I get it, so is this the principal?

    VB Code:
    1. Private Sub MainSub
    2.   Try
    3.   For i = 1 To 500
    4.     TopSub()
    5.   Next
    6.   Catch ex As Exception
    7.     If TypeOf ex.InnerException Is LowerSubException Then
    8.       'Report nasty things
    9.     End If
    10.   End Try
    11. End Sub
    12.  
    13. Private Sub TopSub()
    14.     For i = 1 to 1000
    15.     'Call the lower sub
    16.     LowerSub()
    17.   Next
    18. End Sub
    19.  
    20. Private Sub LowerSub()
    21.   Try
    22.     'Do stuff
    23.   Catch ex As LowerSubException
    24.     'Do nothing here?
    25.   End Try
    26. End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Stopping current execution

    No. Read my post again. I gave two options and that code isn't using either of them.

    Also, you would never use a generic Catch block and then test the type of the exception like that. You write a Catch block specific to the type of exception you want to catch. If you don't care the type of exception then you just catch an Exception object. If you only want to catch a particular type of excepton then you specify that in the Catch statement.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width