CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2004
    Posts
    48

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Problem with 2 try/catch blocks

    Perhaps this is within a loop?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2004
    Posts
    48

    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.

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile 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.

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Problem with 2 try/catch blocks

    Please post the full program. The code you posted doesn't fit to the output.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with 2 try/catch blocks

    Quote Originally Posted by Kaiserle2000 View Post
    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Jul 2004
    Posts
    48

    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

  9. #9
    Join Date
    Apr 2008
    Posts
    725

    Re: Problem with 2 try/catch blocks

    that is not compilable as a complete program.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Problem with 2 try/catch blocks

    Quote Originally Posted by Kaiserle2000 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured