Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Exceptions

Hi everyone,

I've written the following which works no problems :-

try
{
if (std::ifstream x("C:\\x1.txt"))
{
cout << "Here\n";
}
else
{
throw 1;
}

}
catch (int i)
{
cout << i << endl;
}

Is there a nicer way to write this, i.e. with no if else in the try block ?
[402 byte] By [ami] at [2007-11-11 9:59:05]
# 1 Re: Exceptions
I think it can be done simpler without else, but still with if:
try
{
std::ifstream x("C:\\x1.txt");

if( ! x) throw 1;

cout << "Here" << endl;

}
catch (int i)
{
cout << i << endl;
}
Viorel at 2007-11-11 20:59:45 >
# 2 Re: Exceptions
or

try{

if (! fstream("file.txt"))
throw 1;
cout<<"here<<endl;
}
Danny at 2007-11-11 21:00:39 >