如何吞下所有异常并保护我的应用程序免于崩溃? [英] How can I swallow all exceptions and protect my application from crashing?

查看:44
本文介绍了如何吞下所有异常并保护我的应用程序免于崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一些C#应用程序崩溃是由于错误条件引起的,例如 obj = null obj.member = null .很多时候,obj来自3rdPartyApp的界面.并导致3rdPartyApp和MyCsApp一起崩溃.

I've found several C# application crashes in response to error conditions such as obj = null or obj.member = null. A lot of time, the obj from the interface of 3rdPartyApp. And caused both 3rdPartyApp and MyCsApp crashed together.

如何在所有可能的区域中添加异常处理,以便我的应用程序可以在这些灾难性情况下生存下来?将try-catch添加到所有位置并从情况中恢复是一个挑战.

How could I add exception handling in all possible areas so that my application can survive in these disastrous situations? It is a challenge to add try-catch to ALL places, and recover from the situation.

我该如何以现实,可靠和防弹的方式完成此任务?

How could I accomplish this in a way which is realistic, reliable and bullet-proof?

[更新:工业自动化控制]

[Update: Industry Automation Control]

结构:

GUI(asp.net,c ++)-RuntimeApp(C ++)-MyCsApp(cs)-3rdPartyApp(Cs)

GUI(asp.net, c++) - RuntimeApp (C++) - MyCsApp(cs) - 3rdPartyApp(Cs)

正常程序:

  1. HostApp-(通过以太网Cabele连接)-MyCsApp
  2. 操作员-GUI-RuntimeApp-MyCsApp

异常情况:

  1. 一些非标准的操作程序;
  2. 发生了一些硬件问题;

我最好处理所有异常情况.最重要的是,我必须考虑如何从这种情况中恢复过来.

I'd better handle all the abnormall conditions. And most importantly, I must think how to recover from the situations.

推荐答案

首先要治愈该疾病,找出导致崩溃的原因,并确保代码由于 obj = null 崩溃而崩溃

It would make sense to cure the disease first, find out why it's causing to crash, sure the code is crashing because of obj = null or similar - using exception handling and swallowing all exceptions is just masking the problem....That's what it is not used for! It sounds like there's a lot of code-smells that is triggering the crashes - Protecting your application from crashing is not the right way to deal with it and only making things worse...

好吧,您可以按照约翰·桑德斯(John Saunders)和pm100的建议进行操作...但是要以某种方式处理它,以查看根本原因是什么,不要在一天结束时将其视为神奇的灵丹妙药",与第三方应用程序交互的代码需要彻底调试...

Ok, you can follow John Saunders's and pm100's suggestion to do that...but handle it in a manner to see what's the root cause, do not treat it as a 'magic silver bullet', at the end of the day, the code that is interacting with the third party application needs to be debugged thoroughly...

例如


object foo = null;
bar baz;

// ....
// foo is now set by thirdparty app

if (foo != null && foo is bar)  baz = (bar)foo as bar;

if (baz != null){

  // Continue on, baz is a legitimate instance of type 'bar'

}else{

  // Handle it gracefully or throw a *user defined exception*

}

请注意如何使用'as'检查'foo'是否适合'bar'实例的类型-现在与之进行比较,这是一种典型的代码味道...

Notice how the 'as' is used to check if 'foo' is of the right type for 'bar' instance - now compare with this, that is a typical code smell...


object foo = null;
bar baz;

// foo is now set by thirdparty app - ARE YOU REALLY SURE ITS NON-NULL?
// IS IT REALLY OF TYPE 'BAR'?

baz = foo; // CRASH! BANG! WALLOP! KERRUNCH!

这篇关于如何吞下所有异常并保护我的应用程序免于崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆