首页 / C# / c# – 如何尝试/捕获所有异常
c# – 如何尝试/捕获所有异常
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何尝试/捕获所有异常,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1365字,纯文字阅读大概需要2分钟。
内容图文
![c# – 如何尝试/捕获所有异常](/upload/InfoBanner/zyjiaocheng/757/e6edfe20c86049dda998cde812d99316.jpg)
我正在完成由其他人启动的UWP应用程序.该应用程序经常崩溃,我总是在App.g.i.cs中结束
if (global::System.Diagnostics.Debugger.IsAttached)
global::System.Diagnostics.Debugger.Break();
然后,我必须说“不,不要启动调试器”并关闭2个窗口.
有什么地方我可以放一个大尝试/捕获,这样我不必每次都发生时重启应用程序?我在AppShell或App中找不到任何东西.
或者我是否必须在每个事件处理程序中放置一个try / catch?
解决方法:
如果您希望每次遇到未处理的异常时都避免启动新调试器并重新启动应用程序,则可以使用Application.UnhandledException event并将事件参数的Handled属性设置为true,如下所示:
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
this.UnhandledException += (sender, e) =>
{
e.Handled = true;
System.Diagnostics.Debug.WriteLine(e.Exception);
};
}
The UnhandledException event is used to notify the app about exceptions encountered by the XAML framework or by the Windows Runtime in general that have not been handled by app code.
Normally after the UnhandledException event is fired, the Windows Runtime terminates the app because the exception was unhandled. The app code has some control over this: if the UnhandledException event handler sets the 07001 property of the event arguments to true, then in most cases the app will not be terminated.
有关详细信息,请参阅Application.UnhandledException event的备注和博客:Strategies for Handling Errors in your Windows Store Apps.
内容总结
以上是互联网集市为您收集整理的c# – 如何尝试/捕获所有异常全部内容,希望文章能够帮你解决c# – 如何尝试/捕获所有异常所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。