1

I have a lot of code written from an older winforms project that I was working on. I have recently created a method to log errors (to a database) that occur in a couple try/catch blocks. Is there a way that I can add this functionality to the application overall whenever the application moves to catch ex as exception, or if an exception somehow goes unhandled, instead of adding it to all my try/catch blocks?

My method is created by the dataset designer to insert a row into the "Error Table" within my DB and looks like:

tbladapter.LogError("TRY/CATCH", ex.Message & ";;;; " & ex.InnerException.ToString, My.Application.UserID, Now)
Msgbox("Tell genius he needs to learn how to expect and handle errors!")
4

3 回答 3

3

是的,您可以捕获所有异常,即使它们被 try/catch 块捕获。您可以通过处理FirstChanceException当前AppDomain. 为此,当您的应用程序启动时,请调用AddHandler,如下所示:

AddHandler AppDomain.CurrentDomain.FirstChanceException, AddressOf FirstChanceExceptionHandler

这是一个示例处理程序方法:

Private Sub FirstChanceExceptionHandler(sender As Object, e As System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs)
    Console.WriteLine(e.Exception.ToString())
End Sub

另外,我应该提到,使用MsgBox通常被认为是对 VB6 的回归。 MessageBox.Show是更好的选择。此外,您可能不知道简单地调用ToString大多数异常将显示消息、源、堆栈跟踪以及所有内部异常的信息。调用ToString也比访问所有这些单独的属性更容易。

于 2013-09-13T16:04:16.310 回答
0

在 Windows 窗体应用程序中,您可以挂接到某个ThreadException事件。Application只要您的应用程序中发生异常,此方法就会运行。

在创建表单的类(可能是 Program.vb?)中,您可以添加类似

Application.ThreadException += New ThreadExceptionEventHandler(form1.someHandler)

在您的表单中处理它。

于 2013-09-13T15:59:48.730 回答
0

前段时间我在一个 web 项目中实现了类似的东西,并使用了企业库中的异常处理块功能

这里有一个很好的起点,我相信您可以找到其他起点。

从记忆中我认为这很容易实现,因为我是从输出中自动创建支持票证的。

于 2013-09-13T16:04:57.393 回答