Re: Try Catch Finally in MFC
thx Joseph and David,
Here is the situation. At different stages of execution i am creating new
variables. In case there is an
exception i want to release them.
Combining both of your thoughts, it will be wise to
1. Wrap the pointers in a C++ class
2. Handle the exception.
3. In the destructor of the C++ class deallocate pointers if they are not
null.
Advise.
TRY
{
TCHAR *p1 = new TCHAR(100);
....
.....
If(Condition1==TRUE)
return;
TCHAR *p2= new TCHAR(100);
//do something with p1 and p2 pointers,
....
....
....
If(Condition2==TRUE)
return;
TCHAR *p3= new TCHAR(100);
//do something with p1 and p2 pointers and p3 pointers
....
....
....
If(Condition3==TRUE)
return;
}
CATCH(Exception,e)
{
}
"Joseph M. Newcomer" <newcomer@flounder.com> wrote in message
news:9i57a3pmeij1tf40qc4cmsb6f534e72lv9@4ax.com...
There is no "finally" method in MFC, because C++ does not really require
one.
Throwing exceptions is a nominally expensive operation and should be done
only when there
is an error. Your example of an improper parameter is exactly what this
is good for.
Most of MFC works well if your exception class is derived from CException,
e.g.,
class CBadParameterException : public CException {}
you may put additional members in to provide information such as the type
of error, etc.
joe
On Sun, 22 Jul 2007 05:40:26 -0500, "slg" <slg@abc.com> wrote:
Iam using latest MFC Version, i believe 7.0. I could only find TRY CATCH
block in MFC.
How can i implement try -catch- finally in MFC?
One thought i have is to throw an exception while iam in try block when i
want to return.
Is this a good idea?
TRY{
//When i want to return due to improperly formatted parameter to the
function, i will throw a custom exception.
THROW();
}CATCH(Exception,e)
{
}
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm