使用异常进行错误处理 [英] Using exceptions for error handling
问题描述
在使用MFC已有近20年的时间之后,我尝试使用异常的优点.我可以看到在错误情况下递减堆栈的值,而不必在每个级别上传递错误值.有没有办法用错误号来做到这一点,而不必构造CException
派生的类.当然,此机制取决于基础Kernel
API函数.
我现在正在使用的DLL肯定没有任何GDI功能.由于方便访问Kernel32
函数,因此在此使用MFC.我的DLL会尽可能地回答问题并做出回应.它是基于服务器的DLL,提供错误日志.
实际上,它在堆栈展开方面相当宽容,因为它还设计有垃圾收集器,该垃圾收集器会自动删除在创建后的某个时间段(例如15分钟)内未访问的资源.该原型可与setjump
一起很好地使用,但不是多线程的.
我想知道如何使用异常而不是setjmp
来实现这种行为.在2.0版中支持真正的" C ++异常.
根据您的异常处理策略,您可以throw
调整任何内容并catch
执行.在您的情况下,您似乎想要throw
和int
.那应该可以,但是考虑为异常使用更完整的胖类-标准C ++运行时库中的std::runtime_error
效果很好.甚至string
可能比原始的int
更好.
其他几点...
对于MFC(通常是Windows编程),在窗口过程中始终catch
出现任何异常,您应该可以. USER不喜欢通过它摆动的C ++异常.如果要执行多线程编程,请在线程函数的边界处停止所有异常.
还有一点...如果您热衷于使用原始指针,请开始使用某种形式的资源管理类-对于VC ++ 2008和更早版本,使用std::auto_ptr
,对于VC ++ 2010使用std::shared_ptr
或std::unique_ptr
.否,那么您就冒着资源泄漏的风险. GDI对象也是如此-确保它们全部包裹在对象中,如果堆栈展开,它们将删除GDI对象.
希望有帮助,
灰
好吧,
如我之前所说,我从未处理过结构化异常处理.
我无法理解的是,为什么我必须抛出一个Type而不是一个Value.如果我不知道它的值,如何在另一侧捕获一个int.
在我现在正在工作的情况下,MFC Windows实际上是使用"Windows.h"文件编写的大量DLL周围的薄层.
因此,内核如何了解CPP中使用的类型,以及当我仅抛出一个int时如何捕获.
谢谢,请解释一下.我是个厚实的人.
问候,
布拉姆.
CExeption
派生类可以在其中包含用于错误信息的任何值字段,
不能吗? :)
(只需在其构造函数中扩展并填充您的对象.请参阅还... [ 解决方案
You haven''t had to useCException
since VC++ started supporting "real" C++ exceptions with version 2.0.
Depending on your strategy for exception handling, you canthrow
just about anything andcatch
it. In your case, you seem to want tothrow
anint
. That should work but consider using a fuller fat class for your exceptions -std::runtime_error
from the standard C++ runtime library works admirably. Even astring
might be a better idea than just a rawint
.
A couple of other points...
For MFC (and actually Windows programming in general), alwayscatch
any exceptions around your window procedures and you should be okay. USER doesn''t like C++ exceptions wiggling through it. If you are doing multi-threaded programming, then stop all exceptions at the boundaries of your thread function.
One other point... if you''re a fan of using raw pointers, then start using some form of resource management class -std::auto_ptr
for VC++ 2008 and earlier,std::shared_ptr
orstd::unique_ptr
for VC++ 2010. If you don''t, then you risk leaking resources. The same goes for GDI objects - make sure they''re all wrapped up in objects which delete the GDI object if the stack unwinds.
Hope that helps,
Ash
Well,
As I said before, I never dealt with structured exception handeling.I''m very much a Nebe there.
what I cannot get my brain around to, is, Why must I throw a Type, and Not a Value. How do I catch an int on the other side, if I don''t know it''s value.
In the case I''m working at now, MFC Windows is actually a thin layer around a very much larger set of DLL''s written using the ''Windows.h'' File.
So, What then does the Kernel know about types used in CPP, and, how then do I catch when I just throw an int.
Thanks, and please explain.It''s Me who is thick.
Regards,
Bram.
AnCExeption
derived class could have any value fields inside for the error infos,
couldn''t it ? :)
(Just extend and fill yor object in its constructor. See also...[^])
这篇关于使用异常进行错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!