How to ensure a statement be executed in C++? I mean if an exception is thrown before the statement then the statement most likely won't be excuted. For those statements to release the resource, for instance, close the files, it is really bad.
Printable View
How to ensure a statement be executed in C++? I mean if an exception is thrown before the statement then the statement most likely won't be excuted. For those statements to release the resource, for instance, close the files, it is really bad.
That is what try/catch is for :D
Sometimes it's necessary to call your cleanup code from the catch block.
Let me give an example,Quote:
Originally posted by TheCPUWizard
That is what try/catch is for :D
try
{
statement s1;
statement s2;
statement s3;
...
}
catch(someException e)
{
...
}
If statement s1 throws an exception, how do you ensure s3 be excuted? Thanks.
you should be catching your exceptions by constants reference.
try
{
statement s1;
statement s2;
statement s3;
...
}
catch(const someException& e)
{
...
}
Well...by putting it into the 'catch' block... :DQuote:
Originally posted by dullboy
If statement s1 throws an exception, how do you ensure s3 be excuted? Thanks.
No...serious, the first question would simply be why you want to execute statement s3 although statement s1 has failed? How can you ensure that the data (integretity, consistency etc.) is still guaranteed and not broken by the failure of statement s1?
Seperate try/catch block for each statement.Quote:
Originally posted by dullboy
Let me give an example,
try
{
statement s1;
statement s2;
statement s3;
...
}
catch(someException e)
{
...
}
If statement s1 throws an exception, how do you ensure s3 be excuted? Thanks.
In some cases, for example, s1 opens a file, s2 tries to read the file and s3 close the file. if s2 throws an exception, then s3 won't be excuted. In this case, file handle is never close. So based on you guys' answers, I guess I should code like this,Quote:
Originally posted by Andreas Masur
Well...by putting it into the 'catch' block... :D
No...serious, the first question would simply be why you want to execute statement s3 although statement s1 has failed? How can you ensure that the data (integretity, consistency etc.) is still guaranteed and not broken by the failure of statement s1?
try
{
statement s1;
statement s2;
}
catch(const someException& e) // thanks for souldog's comments
{
statement s3;
}
is that correct? Thanks.
try
{
statement s1;
statement s2;
statement s3;
}
catch(const someException& e) // thanks for souldog's comments
{
statement s3;
}
The catch block is only executed if an exception is thrown, so
you need to close the file in both places.
const is not always needed on the exception.
Thanks for your advices. I am also curious in which case const is needed and in which case const is NOT needed?Quote:
Originally posted by souldog
try
{
statement s1;
statement s2;
statement s3;
}
catch(const someException& e) // thanks for souldog's comments
{
statement s3;
}
The catch block is only executed if an exception is thrown, so
you need to close the file in both places.
const is not always needed on the exception.
Well...you should rather use a common idiom named RAII (Resource Acquisition Is Initialization):Quote:
Originally posted by dullboy
So based on you guys' answers, I guess I should code like this,
try
{
statement s1;
statement s2;
}
catch(const someException& e) // thanks for souldog's comments
{
statement s3;
}
is that correct? Thanks.
This will ensure that the file will be always closed... :cool:Code:#include <string>
class CFoo
{
public:
CFoo(const std::string strFilename) { // Open file }
~CFoo() { // Close file }
void Read() { // Read file }
};
int main()
{
try
{
CFoo File("c:\test.txt");
File.Read();
}
catch(...)
{
}
return 0;
}
Well...that would create redundant code...look at my previous reply for a better (in my opinion) solution...Quote:
Originally posted by souldog
try
{
statement s1;
statement s2;
statement s3;
}
catch(const someException& e) // thanks for souldog's comments
{
statement s3;
}
The catch block is only executed if an exception is thrown, so
you need to close the file in both places.
Nice. I like it.Quote:
Originally posted by Andreas Masur
Well...you should rather use a common idiom named RAII (Resource Acquisition Is Initialization):
This will ensure that the file will be always closed... :cool:Code:#include <string>
class CFoo
{
public:
CFoo(const std::string strFilename) { // Open file }
~CFoo() { // Close file }
void Read() { // Read file }
};
int main()
{
try
{
CFoo File("c:\test.txt");
File.Read();
}
catch(...)
{
}
return 0;
}
A little aside :
why should the exceptions be const ?
I'm just interested. I can almost see why, but I'd be interested in the reply.
I always try to learn from these groups and suggestions like these are highly useful.
Many thanks SoulDog.
Darwen.
Oh and to Andreas :
The problem with catch (...) means that when a failure in your program occurs then you won't know why it happened.
A GPF might have occurred (not a file error) and catch (...) will trap this and not pass it on.
I always try to catch only the exceptions which I would normally expect from an operation. If something else happens, then I want to know about it.
Only then can I fix a bug which would normally be hidden by catch(...).
I may be wrong of course....
Darwen.