-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexception.cc
More file actions
50 lines (43 loc) · 730 Bytes
/
exception.cc
File metadata and controls
50 lines (43 loc) · 730 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
std::string stringerror()
{
return strerror(errno);
}
struct SmartFP
{
SmartFP(const char* fname, const char* mode)
{
d_fp = fopen(fname, mode);
if(!d_fp)
throw std::runtime_error("Can't open file: " + stringerror());
}
~SmartFP()
{
fclose(d_fp);
}
FILE* d_fp;
};
void func2()
{
SmartFP fp("nosuchfile", "r");
char line[512];
while(fgets(line, sizeof(line), fp.d_fp)) {
// do things with line
}
// note, no fclose
}
void func()
{
func2();
}
int main()
try {
func();
}
catch(std::exception& e) {
std::cerr<< "Fatal error: " << e.what() << std::endl;
}