gpt4 book ai didi

vb.net - 在应用程序级别处理异常

转载 作者:行者123 更新时间:2023-12-02 01:08:14 24 4
gpt4 key购买 nike

我有一个 vb.net 主桌面应用程序,还有主应用程序的配套应用程序。

我必须对应用程序中的所有方法进行异常处理,但不想在每个方法或事件点击中放入 try catch。

有没有集中处理异常的方法。

其次,我能否避免任何异常并保持应用程序运行而不会出现任何崩溃?我的意思是我可以向用户显示是/否消息而不是崩溃。

最佳答案

第一个问题: 是的,有一个集中的地方可以捕获应用程序级异常。检查应用程序事件:

Namespace My

' The following events are available for MyApplication:
'
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication

Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
' Log it?
' handle it?
End Sub

End Class

End Namespace

关于你的第二个问题,有几种方法可以让你的应用程序在不崩溃的情况下运行:

  1. 捕获未处理的异常并只显示一条消息
  2. 有一个 MDI 父窗口并在子窗体中捕获异常
  3. 使用服务并在服务级别处理异常
  4. 上网查看更多选择....

关于vb.net - 在应用程序级别处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20368699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com