Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Completely halting all code and error handling 1

Status
Not open for further replies.

ElectronikBean

Programmer
Oct 10, 2002
24
0
0
GB
Hello, and thanks for looking at my problem.

I need to find a way of telling the system to stop running all code, a sort of halt completely, other than just 'Exit Sub' or 'Exit Function'.

I have tried using the err.raise method, but this does not actually stop the code from running. Once the error handler has taken care of it, vb still continues to run other code. The only way seems to be to use a boolean variable, or to check for an error number later on in the code. ie.
if FaultFound then exit sub
or,
if err.number <> 0 then exit sub.

This is an effort to keep the vb more object orientated, and have the code run smoother. If an object class raises an error, then it should be able to activate a halt on all code running.

For example purposes this is a kind of snippet of what I am trying to do...
--------
Public WithEvents Object1 as Class1
Private str as String

sub Example()
str = Object1.Property

'Other code doing other things with this property...
end sub
---------

The property get code then finds an error in obtaining the data, and runs an event and raises an error. When finished, the code continues to run the rest of the 'Other code doing other things' as above.

Once an error is raised with that property, the code should just handle the error then stop.

Thanks
E-Bean
 
I may be missing your point, but in your example, doesn't the following do what you want?
[tt]
Public WithEvents Object1 as Class1
Private str as String

Sub Example()
On Error GoTo ErrorTrap
str = Object1.Property

'Other code doing other things with this property...
Exit Sub
ErrorTrap:
' Handle the Error Here
End Sub
[/tt]
 
Thanks Golom, But no.

Public WithEvents Object1 as Class1

Say if the below code is my Form1 class.
----------------------------------------

Private str as String

Sub Example()
On Error GoTo ErrorTrap
str = Object1.Property

'Other code doing other things with this property...
'Do not run the rest of this code of there is a problem getting the data for that property.

Exit Sub
ErrorTrap:
' Handle the Error Here

End Sub

sub object1_getdatafault()
'handle event for this user defined problem


end sub

This would be the object class
-------------------------------
Option Explicit

Public Event getdatafault

Property Get Property() as string

Property = GoGetData "MyData"

End property


Private Function GoGetData() as string
On Error Goto Errorhandler

'....This code does some stuff to return the data but there is a problem obtaining the data soooo... it raises an event to fix the problem and declares an error.

raiseevent getdatafault

err.raise 600, "Error in getting data"



Exit Function
Errorhandler:

msgbox "Error!!!! " & err.number & " " & err.description
'want to stop all processing here to prevent any further actions. However, the system will now run the rest of the property get, and continue running the 'Other code doing other things with this property... - which I do not want it to do...


End Function
 
OK. I think that your problem is that you have an error handler in your class. The expectation is that such an error handler will deal with the error so that higher level routines won't need to. You could try, in your class error trap, doing something like

ErrorHandler:
If Err.Number = ... an error I can deal with locally ... Then
' Deal With It
Else
On Error GoTo 0
' Turn off Local Error Handling
Err.Raise vbObjectError + 999, "Error in getting data"
End If
End Property

That should (I think) cause the system to work back up the error tree until it hits an active error handler which would be the one in the routine that invoked the Class Property. Alternatively, eliminating error handling completely in the Property should also cause the error to bubble up to an available error handler.
 
Good idea Golom, thankyou.

That might work, currently I capture the problem as an event, and the idea of letting the vb error handling work for me may tidy things up. I'll give that a go.

Your a star!

E-Bean
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top