Luc484
February 10th, 2008, 11:29 AM
Hi! I'm trying to write to a file stream, so I open a file I want to write to and I catch the exception I throw in case of problems. Inside the same try block, I throw another exception in same cases, using a different value for the parameter. In that case, I want to write something into the same exact file I opened before. Unfortunately, the variable of type ofstream already declared cannot be used as it is declared in a different block. So I created another variable of type ofstream for the same output file. Is this correct? Can I do this? I mean, can I do this even if the stream of the try block was not closed correctly (the exception is thrown before the stream can be closed)? This is an example:
try
{
ofstream streamOut("output");
if (!streamOut) throw(0);
// do something
... throw(1);
// do something else
streamOut.close();
}
catch(int error)
{
if (error == 0) // do something
else if (error == 1)
{
ofstream streamOut("output");
//write something
streamOut.close();
return 1;
}
}
return 0;
Is this permitted?
Thanks!
try
{
ofstream streamOut("output");
if (!streamOut) throw(0);
// do something
... throw(1);
// do something else
streamOut.close();
}
catch(int error)
{
if (error == 0) // do something
else if (error == 1)
{
ofstream streamOut("output");
//write something
streamOut.close();
return 1;
}
}
return 0;
Is this permitted?
Thanks!