forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAII_fstram.cpp
More file actions
29 lines (25 loc) · 852 Bytes
/
RAII_fstram.cpp
File metadata and controls
29 lines (25 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//
// Created by light on 19-12-9.
//
#include <iostream>
#include <fstream>
// RAII 资源获取即初始化,例2
// C ++保证在对象超出范围时调用析构函数,而不管控制如何离开该范围。
// 即使抛出异常,所有本地对象也会超出范围,因此它们的相关资源将被清除。
void foo() {
std::fstream file("bar.txt"); // open a file "bar.txt"
if (rand() % 2) {
// if this exception is thrown, we leave the function, and so
// file's destructor is called, which closes the file handle.
throw std::exception();
}
// if the exception is not called, we leave the function normally, and so
// again, file's destructor is called, which closes the file handle.
}
int main() {
try {
foo();
} catch (std::exception) {
puts("exception!");
}
}