Reputation: 1573
I have a vb.net program that accesses 10 different databases for reports. I am using a "Try" statement at the end of my program to send the data I build with LINQ from the datasets via email.
The problem i'm running into is when I try to connect to a database that is offline, or times out the program locks up, or ends. What i'd like to do is something similar to "On error resume Next" however, I can't do that with a try statement already in the program... Any suggestions?
Upvotes: 0
Views: 2361
Reputation: 172270
Although Fredou's answer is technically correct, I would suggest that you slightly restructure your program. Instead of
On Error Resume Next
Dim db1 = OpenDbConnection() ' fails
...do something with db1... ' more errors, failing silently, occur here
you ensure that only the operation opening the database is allowed to fail:
Dim db1 As OleDbConnection ' or whatever DB library you happen to use
Try
db1 = OpenDbConnection()
Catch ex As Exception
Console.WriteLine("Skipping DB1: " & ex.ToString())
End Try
If db1 IsNot Nothing
...do something with db1... ' Errors occuring here *are* reported
End If
That way, you are only ignoring errors in certain well-defined parts of your code.
Upvotes: 0
Reputation: 1749
Call your database from a seperate function that returns you a boolean on success or not. Somthing like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
Try
'do something or nothing
If Not OpenConnecttion() Then
End If
Catch generatedExceptionName As Exception
Throw
End Try
End Sub
Private Function OpenConnecttion() As Boolean
'do you database conenction and maybe retrieval stuff here
Try
Catch ex As Exception
Return False
End Try
Return True
End Function
Upvotes: 0
Reputation: 6514
Check out this answer on stackoverflow. It is in C# though
Basically it is an expression which deals with exceptions on the top level.
However, you have to make sure that you wrap each possible code block with it as exceptions are structured.
Upvotes: 0
Reputation: 20100
on error resume next under vb.net;
Try
something()
Catch ex As Exception
'do nothing
End Try
try to find a better way to handle this if this is a long term code / production code
Upvotes: 3