English 中文(简体)
我是否应当对现有法典基础添加例外处理?
原标题:Should I add exception-handling to existing code base?

我很想知道在现有法典中增加例外处理的好处和劣势。

我在视窗环境中控制贺卡的SDK。

SDK由100多个相互交流的DLLs组成。 我们现有的法典基础可能包含100 000条(如果不超过1 000 000条)的法典。 我们的单元也经过了高度多面阅读。

We link with the proper library so that we use nothrow new (lic.lib instead of licp.lib).

大部分法典没有例外处理。 法典的编写考虑到这一点。

int *p = new int[size];
if (p == NULL)
{
   // handle this case...
   // most probably return an error code
}

char *q = new char[size];
if (q == NULL)
{
    delete[] p;
   // handle this case...
   // most probably return an error code
}

我们还使用RAII技术。 例如,我们有一个目标,即自动等待和释放一个关键部分。

我们希望改善我们SDK的稳定。 我们正在考虑增加例外处理,但我不相信这是改善稳定的适当途径。 我必须承认,我没有多少经历过艾滋病毒/艾滋病。

总的来说,该法规定在疏远前对民族解放军点员进行0次或检查。 但这种情况仍然存在。 由于按零划分或贬低全国人民力量的点员不会放弃一个例外,我想知道,如果处理不当,那将会产生100 000条法典线,并增加例外处理,从而改变工作流程,并可能造成记忆泄露。 我曾尝试过SEH,但我认为,开始使用SEH是明智的,Microsoft是特有的,是没有的?

我认为,如果审查现行守则和简单检查可能失事,如可能错失的零。

Also, if I were to add exception-handling, how would I proceed? Modify all the modules at once or start from the bottom-up (meaning, if Module A calls Module B which calls Module C, I would modify C, then B then A since we release our software quite frequently and we would probably only have time to modify C before the next release).

Thank you!

问题回答

I d like to know the advantages and disadvantages of adding exception-handling to existing code.

你不说“例外处理”指的是什么,因此,我首先要谈一些基本内容:标准C++ (请将此问题贴在)requires,请你写成所有三维申请的“手列例例例”,否则你的代码为faulty。 允许C++标准图书馆的各部分提供例外情况,包括贵国样本代码使用的<代码>新。 因此,你的法典已经有可能将例外情形抛在其中,它必须“处理”。 该案发生什么? 基本上,你必须写“,但安全编码除外。

  • It is an error for a program to leak resources in the face of exceptions. You use RAII so you should be OK.
  • It is an error for any object to enter an inconsistent state after an exception is throw. Ensuring that can be much more tricky.

你们首先应当注重使你的法典例外安全。

有了《遗产法》,你就应当在几个地方采用例外处理方式,作为时间表许可;或者在《法典》中,使用最少的领域(将错误风险降低到法典的其余部分)或将产生最大效益的领域(错误之处)。

我不建议搁置一个遗产项目,只是增加各地的例外处理。 遗产法典中最难的部分是修改和保持其运作。 毕竟,它经过测试,其行为有充分记录。

我同意Raedwald的看法,即如果你在使用C++时没有非常谨慎的编码标准来避免使用EH(如使用新电灯、避免标准集装箱等),那么,如果我假定遗产代码的话,则该代码已经破碎,并且有可能在面临例外情况的情况下泄露和断断断裂了已经碰到的物品,例如<代码>bad_alloc或bad_cast,如果你依赖<编码>dynamic_cast<<<>code>。

That 说,从一个具有遗产代码基础的非常务实的观点来看,遗产法有可能消失。 毕竟,在不明确控制记忆分配的情况下,有多少非属地申请能够从<代码>bad_alloc的例外情况中恢复? 并非很多,这并没有导致整个世界陷入ech的停顿。

因此,我实际上不建议重写遗产法,试图在任何地方追捕例外和使用RAII。 你可以在这里利用RAII,在你绝对必须修改的法典中,但我试图寻找理由,不要改变它。 为其进行写作测试,并试图将其稳定下来,使之成为一个黑箱;一个功能的图书馆,在无限期地通过无休止的LOC进行交换时加以使用、维护和改变。

现在,我在这里为这一古老的read子(道歉!)而感到迷惑的主要原因是:

The code, in general, checks for divide by 0 or checks for NULL pointers before dereferencing it. But, it still happens that such a case will happen. Since divide by zero or dereferencing a NULL pointer don t throw an exception [...]

我强烈认为,你应回答“无点人”或“零点”等情况,因为“ 节目错误>。 除非你在关键任务软件中重新工作,如果你想要在软件有诱惑力的情况下进行体面的回收,以便努力降低生活成本的风险,或像这种风险一样,否则,你就不想在方案拟定错误的情况下申请令人宽慰的恢复。 你一般不想这样做的原因是,它有躲藏的 b,保持沉默,使用户能够忽视和围绕他们开展工作,甚至可能不会报告他们。

相反,对于方案错误,你一般应赞成<条码>>>>>srt,其中仅涉及例外情况。 如果这一说法失败,倒数建筑中的软件就会停止,而且通常显示一个错误信息,告诉你说,这些说法没有达到准确的编码线。 这通常是最快捷的旁听器,在回应简要报告时发现和纠正这些方案拟订错误,因此认为可以自由使用<条码>。

Exceptions are most useful for external exceptional events outside of a programmer s control. An example would be reading a file that turns out to be corrupt. That s not within the programmer s control to handle so there it s appropriate to throw and recover. Another example is failing to connect to a server, a user jamming an abort button for an operation that is supposed to finish, etc. Those are exceptional external input events, not programmer mistakes.

从方案错误中恢复的最佳方式,如无分点进入和零分立,是首先发现这些错误(asse)是手法,并写照复制该错误、加以固定和通过测试的检验,并把它称作一天,不放弃例外情况,在留下这些错误时予以追捕。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?