Educn uncaughtexceptiontrue xx try f catch beijing

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: “向上穿过堆栈”去为某 个异常查找对应处理器,叫做“堆栈回退”, 此时,会对所有局部对象调用析构函数 Beijing University of Posts & Telecommunications Beijing [email protected] 资源管理示例 void use_file(const char* fn) void use_file(const char* fn) { FILE* f = open(fn, "r"); { try { FILE* f = fopen(fn, "r"); // 使用f // 使用f资源 } fclose(f); catch(...){ } fclose(f); throw; // 若在使用f资源的过程中产生 } 了异常,则fclose(f)将不会被调 fclose(f); 用,资源释放失败 } // 一种解决方法,但是非常罗嗦 Beijing University of Posts & Telecommunications Beijing [email protected] 资源管理示例 考虑问题的一般形式: void acquire() { // 申请资源1 // ... // 申请资源n // 使用资源 // 释放资源n // ... // 释放资源1 } // 一般是按照申请的相 反顺序来释放资源,这 和构造和析构对象的顺 序很相似 定义一个类File_ptr class File_ptr{ FILE* p; public: File_ptr(cons...
View Full Document

This note was uploaded on 02/10/2014 for the course ECE C++ taught by Professor Xiaoshengtang during the Fall '13 term at BUPT.

Ask a homework question - tutors are online