-
February 26th, 2009, 06:38 PM
#1
[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.
-
February 27th, 2009, 01:48 AM
#2
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.
-
February 27th, 2009, 02:50 AM
#3
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.
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?
-
February 27th, 2009, 04:34 AM
#4
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.
-
February 27th, 2009, 06:45 AM
#5
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.
-
March 2nd, 2009, 12:16 PM
#6
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.
-
March 2nd, 2009, 02:18 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|