为什么C++的SEH不提供finally

因为C++支持一种更棒的方法:"resource acquisition is initialization"技术。基本思想是使用局部对象代理资源,局部变量在析构时将会自动释放资源。用这种方式,这样程序开发人员将不会遗忘资源的释放


 // wrap a raw C file handle and put the resource acquisition and release
    // in the C++ type's constructor and destructor, respectively
    class File_handle {
        FILE* p;
    public:
        File_handle(const char* n, const char* a)
            { p = fopen(n,a); if (p==0) throw Open_error(errno); }
        File_handle(FILE* pp)
            { p = pp; if (p==0) throw Open_error(errno); }
        ~File_handle() { fclose(p); }
        operator FILE*() { return p; }   // if desired
        // ...
    };
    // use File_handle: uses vastly outnumber the above code
    void f(const char* fn)
    {
        File_handle f(fn,"rw"); // open fn for reading and writing
        // use file through f
    } // automatically destroy f here, calls fclose automatically with no extra effort
      // (even if there's an exception, so this is exception-safe by construction)

在一个系统中,最糟糕的方式是我们需要为每一个资源设计一个"resource handle"类。然而,我们不是必须用一个"finally"来处理资源。在一个现实的系统中,资源的处理代码远远超过了各种资源的数量,因此,使用“resource acquisition is initialization”技术来处理比使用"finally"结构更加简练。


Because C++ supports an alternative that is almost always better: The “resource acquisition is initialization” technique. The basic idea is to represent a resource by a local object, so that the local object’s destructor will release the resource. That way, the programmer cannot forget to release the resource. For example:


  
  
  1. // wrap a raw C file handle and put the resource acquisition and release
  2. // in the C++ type's constructor and destructor, respectively
  3. class File_handle {
  4. FILE* p;
  5. public:
  6. File_handle(const char* n, const char* a)
  7. { p = fopen(n,a); if (p==0) throw Open_error(errno); }
  8. File_handle(FILE* pp)
  9. { p = pp; if (p==0) throw Open_error(errno); }
  10. ~File_handle() { fclose(p); }
  11. operator FILE*() { return p; } // if desired
  12. // ...
  13. };
  14. // use File_handle: uses vastly outnumber the above code
  15. void f(const char* fn)
  16. {
  17. File_handle f(fn,"rw"); // open fn for reading and writing
  18. // use file through f
  19. } // automatically destroy f here, calls fclose automatically with no extra effort
  20. // (even if there's an exception, so this is exception-safe by construction)

In a system, in the worst case we need a “resource handle” class for each resource. However, we don’t have to have a “finally” clause for each acquisition of a resource. In realistic systems, there are far more resource acquisitions than kinds of resources, so the “resource acquisition is initialization” technique leads to less code than use of a “finally” construct.

Also, have a look at the examples of resource management in Appendix E of TC++PL3e.



https://isocpp.org/wiki/faq/exceptions#finally

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值