|
-
Feb 8th, 2007, 07:16 PM
#1
[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:
Private Sub MainSub
For i = 1 To 500
TopSub()
Next
End Sub
Private Sub TopSub()
For i = 1 to 1000
'Call the lower sub
LowerSub()
Next
End Sub
Private Sub LowerSub()
Try
'Do stuff
Catch
'I want to be able to terminate any further execution of "Main"
'from here if things go badly wrong (but leave the form up)
End Try
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.
-
Feb 8th, 2007, 07:23 PM
#2
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.
-
Feb 8th, 2007, 08:20 PM
#3
Re: [2005] Stopping current execution
ok, I think I get it, so is this the principal?
VB Code:
Private Sub MainSub
Try
For i = 1 To 500
TopSub()
Next
Catch ex As Exception
If TypeOf ex.InnerException Is LowerSubException Then
'Report nasty things
End If
End Try
End Sub
Private Sub TopSub()
For i = 1 to 1000
'Call the lower sub
LowerSub()
Next
End Sub
Private Sub LowerSub()
Try
'Do stuff
Catch ex As LowerSubException
'Do nothing here?
End Try
End Sub
-
Feb 8th, 2007, 08:25 PM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|