0

我有一个 vb.net 程序,它可以访问 10 个不同的数据库以获取报告。我在程序末尾使用“Try”语句通过电子邮件从数据集中发送我使用 LINQ 构建的数据。

我遇到的问题是当我尝试连接到脱机的数据库时,或者程序锁定超时或结束时。我想做的是类似于“On error resume Next”的事情,但是,我不能用程序中已有的 try 语句来做到这一点......有什么建议吗?

4

4 回答 4

3

在 vb.net 下错误恢复下一个;

    Try
        something()
    Catch ex As Exception
        'do nothing
    End Try

如果这是长期代码/生产代码,请尝试找到更好的方法来处理此问题

于 2013-04-16T13:31:51.983 回答
0

在stackoverflow上查看这个答案。虽然它在 C# 中

忽略 C# 中的异常

基本上它是一个处理顶层异常的表达式。

但是,您必须确保将每个可能的代码块都包装起来,因为异常是结构化的。

于 2013-04-16T13:37:49.047 回答
0

从一个单独的函数调用您的数据库,该函数返回一个布尔值是否成功。像这样的东西:

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

结束子

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
于 2013-04-16T13:37:59.047 回答
0

尽管 Fredou 的回答在技术上是正确的,但我建议您稍微重组您的程序。代替

On Error Resume Next
Dim db1 = OpenDbConnection()        ' fails
...do something with db1...         ' more errors, failing silently, occur here

您确保只允许打开数据库的操作失败:

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

这样,您只会忽略代码中某些定义明确的部分中的错误。

于 2013-04-16T13:46:46.737 回答