Re: Why is RAII called RAII?
On Sep 14, 1:22 pm, Goran Pusic <gor...@cse-semaphore.com> wrote:
3. (correct code)
CFile f(params);
use(f);
// Sometimes the inability to open a file can be handled locally
// What about this use case?
static const char* filelist[3] =
{"file1.txt","file2.txt","file3.txt"};
ifstream foo;
for(int idx=0;idx<3;++idx){
foo.open(filelist[idx],ifstream::in)
if(foo.isopen()) {
// frobnicate file contents
} else {
std::cerr << filelist[idx] << " cannot be opened" << std::endl;
}
}
// versus
static const char* filelist[3] =
{"file1.txt","file2.txt","file3.txt"};
for(int idx=0;idx<3;++idx){
try {
ifstream(filelist[idx],ifstream::in)
// frobnicate file contents
}
catch(std::exception&e) {
std::cerr << filelist[idx] << " " << e.what() << std::endl;
}
}
// Consider writing a Unix CLI program that takes a list a of files
// on which to operate as arguments (like 'ls', or 'rm' or
// other obscurities). Would you *really* write that using
exceptions
// to catch non-existent files?