Stop completely after an error catch

Need4Speed

Member
Joined
Apr 4, 2007
Messages
8
Programming Experience
Beginner
Hi..

In my code I call for 3 mudules to be executed like:
OpenDbConnection()
ReadFromDb()
CloseDbConnection()
If an error occures in the first module I can catch that and display an error message. But what I also wanna do in the first module itself, is make sure that the queued up instuctions like the second and the third module don't get executed either. So when an error occures I want the app to show an error message and after that do absolutely nothing else.
How can I do this?

Thx..
 
I don't really get what you're asking, none of the others should execute once an error is found
 
Public Sub OpenDbConnect()
Try
con.Open()
Catch ex As SqlException
MessageBox.Show(
"blablabla.", "title", MessageBoxButtons.OK)
'some code here to make sure that nothing else happens after the message
End Try
End Sub
 
You could make them Boolean functions:
VB.NET:
Sub processes()
    If Not OpenDbConnection() Then Return
    If Not ReadFromDb() Then Return
    If Not CloseDbConnection() Then Return
End Sub
 
Function OpenDbConnection() As Boolean
Try
    '...
Catch
    MsgBox("db connection errored")
    Return False
End Try
Return True
End Function
Or TryCatch the whole block if you don't need to know which method failed:
VB.NET:
Sub processes()
Try
    OpenDbConnection()
    ReadFromDb()
    CloseDbConnection()
Catch
    MsgBox("something went wrong")
End Try
End Sub
 
Sub OpenDbConnection()
    '... don't TryCatch locally
End Sub
 
Thanks, but OpenDbConnection has already been used a big amout of times in the project. I'm looking for a piece of code which can do what I want to be done in OpenDbConnection itself without having to edit the rest of the code in the project. Is that possible?
 
You could set a class level Boolean variable in OpenDbConnection indicating success and check for this from those callers that need to know.
 
Back
Top