-
January 12th, 2011, 01:41 AM
#1
Problem with 2 try/catch blocks
Hello together,
I have a problem with several try/catch blocks in a method.
Code:
try
{
CStringArray* m_TableValues;
m_TableValues = new CStringArray[2];
delete m_TableValues;
}
catch (...)
{
CDebug::showException(__FILE__, __LINE__);
}
wprintf(_T("xxx\n"));
try
{
char* pEmpty = NULL;
*pEmpty = 'x';
}
catch (...)
{
CDebug::showException(__FILE__, __LINE__);
}
The showException() method shows just an AfxMessageBox().
When I start the programm I get the first exception. After that "xxx" is printed. So far so good. But then the second try will be executed after that again the first catch block! Why?
This will cause an ivinite loop because the second try block jumps always in the first catch block and I get many "xxx" in output.
How could I avoid that?
Kaiserle2000
-
January 12th, 2011, 01:56 AM
#2
Re: Problem with 2 try/catch blocks
Perhaps this is within a loop?
-
January 12th, 2011, 02:04 AM
#3
Re: Problem with 2 try/catch blocks
There are 2 try/catch blocks which are called one after the other between a wprintf() function.
There is definitively no loop around this.
The second try block jumps always into the first catch block.
The code in and after the second catch block is never executed.
Maybe problem are the two "catch(...)" blocks?
Last edited by Kaiserle2000; January 12th, 2011 at 02:26 AM.
-
January 12th, 2011, 02:29 AM
#4
Re: Problem with 2 try/catch blocks
The first try-catch block always will succeed cause there is no error. So you always see the exception thrown in th second block invoked by dereferencing of a NULL pointer. If the whole thing is in a loop it will go infinite.
-
January 12th, 2011, 02:52 AM
#5
Re: Problem with 2 try/catch blocks
Please post the full program. The code you posted doesn't fit to the output.
-
January 12th, 2011, 05:24 AM
#6
Re: Problem with 2 try/catch blocks
Originally Posted by Kaiserle2000
Ok,
here is a better code:
First of all, fix your program.
Code:
m_TableValues = new CStringArray[2];
delete m_TableValues;
That delete is the wrong form of delete.
Code:
delete [] m_TableValues;
If you allocate with new[], you must deallocate with delete[], not delete.
Second, your program leaks memory. You allocate the pointer locally in the try block, but have no way to deallocate that memory since the pointer is only local to the try block.
If you're going to test try/catch, at the very least write a program that has no issues that may affect your testing.
Also, is this your full program, or did you lift this code from a larger program? Please post a full program to ensure that this is the actual code you're testing and not part of a larger program (which may indeed have some sort of loop going on).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; January 12th, 2011 at 05:27 AM.
-
January 12th, 2011, 07:22 AM
#7
Re: Problem with 2 try/catch blocks
Well, if you are certain that there is no loop, then...
I suggest that you post the smallest and simplest compilable program that demonstrates the problem. Also, post the input, expected output and actual output.
-
January 12th, 2011, 07:32 AM
#8
Re: Problem with 2 try/catch blocks
Ok,
here is a better code:
Code:
wprintf(_T("xxx\n"));
try
{
wprintf(_T("t1\n"));
CStringArray* m_TableValues;
m_TableValues = new CStringArray[2];
delete m_TableValues;
}
catch (...)
{
wprintf(_T("c1\n"));
// CDebug::showException(__FILE__, __LINE__);
}
wprintf(_T("yyy\n"));
try
{
wprintf(_T("t2\n"));
char* pEmpty = NULL;
*pEmpty = 'x';
}
catch (...)
{
wprintf(_T("c2\n"));
//CDebug::showException(__FILE__, __LINE__);
}
wprintf(_T("zzz\n"));
and here is the apropriate output:
Code:
xxx
t1
c1
yyy
t2
c1
yyy
t2
c1
yyy
t2
c1
yyy
t2
c1
yyy
t2
c1
yyy
t2
c1
yyy
t2
..... infinite loop
I expect following:
Code:
xxx
t1
c1
yyy
t2
c2
zzz
-
January 13th, 2011, 06:18 AM
#9
Re: Problem with 2 try/catch blocks
that is not compilable as a complete program.
-
January 13th, 2011, 07:42 AM
#10
Re: Problem with 2 try/catch blocks
Originally Posted by Kaiserle2000
Ok,
here is a better code:
It is not better code.
It still has the issues I pointed out, and it is not a complete program.
A complete program is one where it can be copied from the message window and pasted directly into our compiler editor, and without any modification, compiled, linked, and tested.
The reason why this is important is that we are not filling in gaps you left out trying to duplicate your issue.
Regards,
Paul McKenzie
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
|