简体   繁体   English

WPF 应用程序的异常处理

[英]Exception handling for WPF Application

I have a substanital WPF application set up as follows: Views, ViewModels, Business Objects and Dals, with a SQL Compact DB.我有一个基本的 WPF 应用程序设置如下:视图、视图模型、业务对象和 Dals,带有 SQL 紧凑型数据库。 Database calls are made via Linq2Sql.数据库调用是通过 Linq2Sql 进行的。 Application is single user.应用程序是单用户。 (one db per user). (每个用户一个 db)。

Assuming that an exception occurs within the Dal during an CRUD operation or a user has deleted the database, where should the exception handling take place.假设在 CRUD 操作期间 Dal 内部发生异常或用户删除了数据库,那么异常处理应该发生在哪里。

Also if for whatever reason a exception is raised within the View/ViewModel I don't want the system to crash.此外,如果出于某种原因在 View/ViewModel 中引发异常,我不希望系统崩溃。 How should I handle this.我该如何处理。 Should I just inform the user that an error occurred and recreate the View/ViewModel so that the user can continue.我是否应该只通知用户发生错误并重新创建 View/ViewModel 以便用户可以继续。 The ViewModels only communicate to other ViewModels via Messenging (eg something needs to be refreshed). ViewModel 仅通过消息传递与其他 ViewModel 通信(例如,需要刷新某些内容)。

In the OnStartup() method of your WPF application you can subscribe to the Application.Current.DispatcherUnhandledException exception event which allows you to record the exception, inform the user of the exception and suggest any remedies if appropriate, and cancel the termination of the application by setting e.Handled = true .在您的 WPF 应用程序的OnStartup()方法中,您可以订阅Application.Current.DispatcherUnhandledException异常事件,该事件允许您记录异常,通知用户异常并建议任何适当的补救措施,并取消应用程序的终止通过设置e.Handled = true

You should only cancel the termination of your application if you can remedy the cause of the exception, otherwise you will potentially leave the application in an undefined state.只有在可以纠正异常原因的情况下,您才应该取消终止应用程序,否则您可能会将应用程序留在未定义的 state 中。

simplest answer would be it really depends what exactly are the requirements of the applications.最简单的答案是它真的取决于应用程序的确切要求。

Details: First of all you should do exceptional handling where ever there is a chance of an exceptional case.详细信息:首先,您应该在有可能发生特殊情况的情况下进行特殊处理。 While performing a CRUD operation, while performing something in view model or while even creating views.在执行 CRUD 操作时,在视图 model 中执行某些操作时,甚至在创建视图时。 User definitely won't like the application to crash.用户绝对不希望应用程序崩溃。 So the best thing to do is to handle the exception, customize the error message so that it can make sense to the user and show it to user so that s/he knows an error has been occurred.所以最好的办法是处理异常,自定义错误消息,使其对用户有意义并将其显示给用户,以便她/他知道发生了错误。 Also ask user what to do now(it really depends on application requirement again) but for example if databse file has been deleted ask user if he wants to create a new one or things like that还询问用户现在要做什么(这实际上再次取决于应用程序要求),但例如如果数据库文件已被删除,请询问用户是否要创建一个新文件或类似的东西

DB problems should be handled in your dal, the dal should inform someone about problem with messaging/events.数据库问题应该在你的 dal 中处理,dal 应该通知某人有关消息/事件的问题。

exceptions from view/view models should be caught inside this classes unless you have some mvvm framework like caliburn, that can ahndle this stuff independently.视图/视图模型的异常应该在这个类中捕获,除非你有一些像 caliburn 这样的 mvvm 框架,它可以独立地处理这些东西。 After catching exception a message should be sent to the shell/view manager that will handle the problems.捕获异常后,应将消息发送到将处理问题的外壳/视图管理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM