windows窗体应用程序_.NET Windows窗体应用程序中的集中式异常处理

windows窗体应用程序

There is an easy way, in .NET, to centralize the treatment of all unexpected errors.

.NET中有一种简单的方法来集中处理所有意外错误。

First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to that Sub in the Application tab of the project's Properties Window (last entry in the Project menu). You must make sure that Enable application framework is unchecked for this to work, because what that does is simply write a Sub Main for you, and you cannot have two of these in the same application.

首先,您需要在模块中编写一个称为Main的Sub,而不是直接在Form中启动应用程序。 然后,在项目的“属性”窗口(“项目”菜单中的最后一个条目)的“应用程序”选项卡中,将“启动对象”设置为该Sub。 您必须确保未选中“ 启用应用程序框架”以使其正常工作,因为这样做只是为您编写一个Sub Main,并且您不能在同一应用程序中拥有其中的两个。

Professional programmers use that approach for most applications, because it enables them to check the environment and set things up before loading the main Form. It also enables you to launch the application in different ways on in different Forms, depending on the situation, command line arguments or the user.

专业的程序员将这种方法用于大多数应用程序,因为它使他们能够在加载主窗体之前检查环境并进行设置。 它还使您可以根据情况,命令行参数或用户,以不同的形式以不同的形式启动应用程序。

But over all, it enables you to trap Exception events at the application level. Here is the basic code for that module.

但总的来说,它使您可以在应用程序级别捕获异常事件。 这是该模块的基本代码。

Module Module1

 Public Sub Main()

    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
    AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler

    Application.Run(New Form1)

 End Sub

 Private Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
    YourReportingMethod(CType(e.ExceptionObject, Exception))
 End Sub

 Private Sub ThreadExceptionHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
    YourReportingMethod(e.Exception)
 End Sub

 Private Sub YourReportingMethod(e As Exception)
    MessageBox.Show(e.Message)
    Application.Exit
 End Sub

End Module

You will end up in that routine anytime an Exception is triggered in code that is not inside of a Try...Catch block. Note that it receives the Exception as an argument, so you have all the necessary information needed to help you pinpoint what happened. If you install a Debug version of your application on your user's computer, the StackTrace will also pinpoint the exact line of code where the error occured. This can be useful when distribute your first few versions that always contain unexpected problems.

每当不在Try ... Catch块内的代码中触发异常时,您都将最终进入该例程。 请注意,它接收Exception作为参数,因此您具有所有必要的信息来帮助您确定发生了什么。 如果您在用户计算机上安装了应用程序的Debug版本,则StackTrace还将查明发生错误的确切代码行。 在分发始终包含意外问题的前几个版本时,此功能很有用。

You can also make it a point, when you have a Try...Catch, to systematically call that routine whenever an unexpected Exception is triggered. You can do it with blocks that end like the following:

当您拥有Try ... Catch时,也可以在每次触发意外的Exception时系统地调用该例程。 您可以使用结尾如下的块来完成此操作:

Try
   'Your code
Catch ex As SqlException
   'SQL Exception handling
Catch ex As IO.EndOfStreamException
   'Unexpected end of the file
Catch ex As IOException
   'File Exception handling
Catch
   Throw
End Try
Catch...Throw at the end does the trick. It will send the Exception up the stack trace. If you terminate all your Try...Catch blocks that way, unhandled exceptions will always bubble up to your centralized error handling routine.

You will see some programmers Catch ex As Exception and Throw ex instead of a simple Catch/Throw. This works, but resets the StackTract to the line on which you call Throw ex. You won't be able to know what happened before the Throw, only the exception that was triggered. This is not very useful, so go for the Catch/Throw.

您将看到一些程序员将

Some programmers do not like that approach, because the Exception that bubbles up could be trapped by another Try...Catch up the calls line. If this is your way of thinking, simply make your reporting method Public instead of Private as I did, Catch ex As Exception instead of simply Catch, and call your routine, passing it the ex that you catched.

一些程序员不喜欢这种方法,因为冒泡的异常可能被另一个Try ... Catch up调用行捕获。 如果这是您的想法,只需将您的报告方法设置为Public而不是私有方法,将

This is a .NET technique, so it also works in C# and most languages supported for Windows Applications.

这是一种.NET技术,因此也可以在C#和Windows应用程序支持的大多数语言中使用。

翻译自: https://www.experts-exchange.com/articles/15559/Centralized-Exception-handling-in-NET-Windows-Forms-Applications.html

windows窗体应用程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值