Let's say I need to perform several read or write operations on a stream and throw an exception if any of them ended with an error. Is there any difference between the following two ways:
{
std::ifstream ifs("filename");
int i;
std::string s;
long l;
//all variables are local, so I'm not interested in them in case of exception
//first way
if(!ifs >> i) throw runtime_error("Bad file");
if(!std::getline(ifs, s)) throw runtime_error("Bad file");
if(!ifs >> l) throw runtime_error("Bad file");
//second way
ifs >> i;
std::getline(ifs, s);
ifs >> l;
if(!ifs) throw runtime_error("Bad file");
//do something with variables
}
If there is no difference, then are there any pitfalls in similar cases which I should know?