CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    [RESOLVED] Need help with strange try..catch behavior

    Take a look at this program. I am having trouble understanding what is happening in the catch. When I run the program the value printed in the catch block is not what I expect. It was incremented in the try and the value of 10 caused the exception. Yet the value printed was 9! It seems like it has something to do with the fact that pos is a function parameter because if I try this where the pos variable is actually created within the function body it works as I expect.

    Code:
    #include <iostream>
    #include <vector>
    void printValue(std::vector<int>& theContainer, std::vector<int>::size_type pos)
    {
        try
        {
    	pos++;
    	std::cout << "the value at index " << pos << "is " << theContainer.at(pos);
        }
        catch(...)
        {
           std::cout << "Caught exception! Index: " << pos << std::endl;
        }
    }
    int main()
    {
    
        std::vector<int>::size_type index(9);
        std::vector<int> theVector(10, 1);
        
        printValue(theVector, index);
        
        return 0;
    }
    Does anyone know why this is happening? Is it normal and should this behavior occur in all runtime environments? I use the green hills ada multi c++ compiler for windows xp and I've duplicated this using both a windows and power pc run time environment. I wonder if this program behaves this way in other run time environments.

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Need help with strange try..catch behavior

    I tried the code in Visual Studio and it prints 10.

    So its probably something to do with how the compiler implements SEH.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

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

    Re: Need help with strange try..catch behavior

    The MinGW port of g++ 3.4.5 compiles the program such that I get the expected output of 10 too.

    Quote Originally Posted by _Superman_
    So its probably something to do with how the compiler implements SEH.
    Isn't this just an ordinary C++ exception (std::out_of_range) rather than an SEH exception that is thrown and caught here?
    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

  4. #4
    Join Date
    Jan 2006
    Posts
    3

    Re: Need help with strange try..catch behavior

    I tried it in HP-UX machine and with aCC compiler and it went fine, printing required result.
    Can you tell us what is the machine/compiler you are using, so that it can be found out.

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Need help with strange try..catch behavior

    On msvc9 on winxp in both debug and release mode the code correctly prints 10.

    Seems you may have found a bug in your compiler.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Need help with strange try..catch behavior

    Thanks, I was able to try this at home with visual C++ and the problem can't be duplicated with visual c++. Since no one else can duplicate it, it must be compiler specific behavior. I'm using a green hills compiler called AdaMulti. I decided to contact technical support for green hills and have received a response so i am working with them to figure out why this is happening. So far, the only suggestion I have received is to increment the variable before entrying the try block which seems to work. I don't really understand why I should have to do that though.

  7. #7
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Resolved Re: [RESOLVED] Need help with strange try..catch behavior

    The tech support team indicated that it was in fact a defect with that particular compiler. Thanks to all who attempted to duplicate this on another compiler for me.

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