-
September 2nd, 2009, 10:16 PM
#1
__try and CStringArray conflict
I'm so tired of this error, and I hope someone can clarify it for me. Say, I have the following:
Code:
CStringArray arr;
...
__try
{
}
__finally
{
}
For some reason this combination produces an error:
Cannot use __try in functions that require object unwinding
I end up not using the exception handling at all to make it go away, but that's not right. What shall I do instead?
-
September 2nd, 2009, 10:59 PM
#2
Re: __try and CStringArray conflict
Why not keep it standard with try/catch(...)?
gg
-
September 2nd, 2009, 11:22 PM
#3
Re: __try and CStringArray conflict
Because it doesn't have __finally.
-
September 3rd, 2009, 12:01 AM
#4
Re: __try and CStringArray conflict
With the proper use of RAII, __finally is not necessary in a C++ program. If an object is destroyed, then its destructor should do the clean up.
Regards,
Paul McKenzie
-
September 3rd, 2009, 07:33 AM
#5
Re: __try and CStringArray conflict
I think the issue is that
__try / __except / __finally
Are for structured exception handling (hardware errors and OS errors, such as division by zero, accessing uncommitted memory etc).
while try/catch is for C++ errors, where some other piece of code explicitely did a 'throw <something>'.
try/catch should not trigger on OS exceptions (an older version of VC++ did), and __try/__except doesn't work with C++ exceptions
They serve distinct purposes...
try/catch does stack unwinding, __try does not, this is why the compiler rightfully tells you what you're trying can't be done.
You'll need to change the way the function is programmed to get this to work.
In this case, you could new the CStringArray rather than have it as a local object. and delete it at the end of the function yourself. But that violates the RAII rule of course. But then the big issue of course is that RAII and SEH don't work well together in any cas eyou turn it.
-
September 3rd, 2009, 08:56 AM
#6
Re: __try and CStringArray conflict
More specifically, I was referring to "/EHa" (asynchronous exception handling model).
Code:
#include <iostream>
using namespace std;
struct bar
{
int m_n;
explicit bar(int n) : m_n(n) {cout << "bar(" << m_n << ")" << endl;}
~bar() {cout << "~bar(" << m_n << ")" << endl;}
};//bar
void foo()
{
bar b(2);
int *p = 0;
*p = 0;
}//foo
int main()
{
try
{
bar b(1);
cout << "Calling foo" << endl;
foo();
}//try
catch (...)
{
cout << "OOPS" << endl;
}//catch
return 0;
}//main
Code:
bar(1)
Calling foo
bar(2)
~bar(2)
~bar(1)
OOPS
http://msdn.microsoft.com/en-us/library/1deeycx5.aspx
gg
-
September 4th, 2009, 04:12 AM
#7
Re: __try and CStringArray conflict
Catching Structured exceptions via the C++ exception model is a REALLY bad idea. I strongly suggest you never ever use it. I also suggest you never ever use catch (...).
Using it in test code is ok enough, but when used in production/release code, you're only going to make you pull out your hair in agony when things start going bad.
We had a big discussion on catch(...) a while back, and I strongly disagree with any argument you can give for it's possible 'good' use. Catching an exception but then not being able to give even the slightest hint at what actually went wrong is just signs of a bad programmer, or being bad at choosing to bet your personal reputation on a library that is badly designed.
-
September 4th, 2009, 07:52 AM
#8
Re: __try and CStringArray conflict
>> Catching Structured exceptions via the C++ exception model is a REALLY bad idea.
??? I fail to see any evidence supporting such a strong statement...
If an exception is thrown, you either catch it or you don't. Choosing how you catch it is of little consequence.
gg
-
September 4th, 2009, 04:04 PM
#9
Re: __try and CStringArray conflict
Thank you, guys. But I'm kinda confused. What is the difference between the following?
Code:
try{
}
catch(...){
}
Code:
__try{
}__exception(){
}__finally{
}
and
-
September 4th, 2009, 04:11 PM
#10
Re: __try and CStringArray conflict
-
September 4th, 2009, 04:25 PM
#11
Re: __try and CStringArray conflict
OK, thanks. Very useful info! It doesn't let me rate you for some reason.
But, it doesn't help my confusion. I quickly looked through those links you gave, and my question is, which one shall I use?
Say, if I use TRY/CATCH type, does it mean that it will catch only MFC-type exceptions? Or do I need to encase it into a try/catch or __try/__exception/__finally block?
Don't get me wrong, but all that exception handling is quite annoying and I'd greatly appreciate if I could use only one set of commands for all the situations. You see my point?
-
September 4th, 2009, 05:16 PM
#12
Re: __try and CStringArray conflict
I can't speak for MFC's macro's. Otherwise I would only use SEH if you have a really good reason for doing so. Utilizing __finally (in C++) isn't one of them.
gg
-
September 5th, 2009, 02:40 AM
#13
Re: __try and CStringArray conflict
Originally Posted by Codeplug
>> Catching Structured exceptions via the C++ exception model is a REALLY bad idea.
??? I fail to see any evidence supporting such a strong statement...
because you can only catch them with catch (...) which means you have absolutely no indication WHY or where it failed.
If you get called middle of the night to go fix a "undefined error occurred" in a important running project, you'll probably change your mind as well. Especially if you then figure out those undefined errors can happen in any of several dozen locations.
If an exception is thrown, you either catch it or you don't. Choosing how you catch it is of little consequence.
well this is where I disagree.
If you can catch what was thrown, then you catch that thing specifically.
If you can't catch what was thrown and resort to a big catchall, you're better off not catching at all.
I'm actually using the "what is a good reason to use catch (...)" in my job interviews for new programmers, it's amazing what kind of answers you get, and stomping out 'bad' practice is the first thing we do in the job training when they do get hired.
-
September 5th, 2009, 02:42 AM
#14
Re: __try and CStringArray conflict
Originally Posted by ahmd
Thank you, guys. But I'm kinda confused. What is the difference between the following?
Code:
try{
}
catch(...){
}
Code:
__try{
}__exception(){
}__finally{
}
and
don't use TRY/CATCH, it's obsolete
use try/catch for catching C++ errors
use __try __except __finally to handle hardware/OS exceptions.
-
September 5th, 2009, 03:26 AM
#15
Re: __try and CStringArray conflict
Originally Posted by OReubens
don't use TRY/CATCH, it's obsolete
use try/catch for catching C++ errors
use __try __except __finally to handle hardware/OS exceptions.
That's the kind of an answer I needed. Thank you!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|