Solved

Unable to catch STL acess violation exceptions in VS2005

Posted on 2007-03-22
4
471 Views
Last Modified: 2010-07-27
Hi Guys,

I am using Visual Studio 2005 and have "Yes With SEH Exceptions (/EHa)" for project properties: C++-->Code Generation-->Enable C++ Exceptions.

But I am not able to catch the following Microsoft STL vector access violation exception error. I am able to catch normal null object pointer access violation exceptions.

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
      try
      {
            string sTmpString ;
            vector<string> vTmpTokens;
            vTmpTokens.push_back("T");
            sTmpString = vTmpTokens[1].c_str();

      }
      catch (...)
      {
            cout<<"Caught an access violation";
            return 1;
      }
}

Thank you,
Kiran
0
Comment
Question by:palliki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18777217
You cannot catch access violations as STL exceptions, since they are SEH exceptions unles you provide a translator, e.g. like in http://msdn2.microsoft.com/en-us/library/5z4bw5h5(VS.80).aspx


#include <windows.h>
#include <eh.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
    cout <<  "In trans_func." << endl;
    throw std::exception;
}

int main()
{
      try
      {
            _set_se_translator( trans_func );

            string sTmpString ;
            vector<string> vTmpTokens;
            vTmpTokens.push_back("T");
            sTmpString = vTmpTokens[1].c_str();

      }
      catch (...)
      {
            cout<<"Caught an access violation";
            return 1;
      }
}
0
 
LVL 3

Accepted Solution

by:
Darrylsh earned 125 total points
ID: 18777302
use at() instead of [ ]

so instead of:

sTmpString = vTmpTokens[1].c_str();

you should use

sTmpString = vTmpTokens.at(1).c_str();
0
 

Author Comment

by:palliki
ID: 18777763
Hi Darrylsh,

Can you please explain the reason?
The operator [] and the function at() both return a reference to the vector element at a specified position. what's the difference?

Thanks a lot,
Kiran
0
 
LVL 3

Expert Comment

by:Darrylsh
ID: 18782642
The difference is that at() does bounds checking and throws an exception when you give it an invalid index. As a trade-off for speed, [] does not do any bounds checking and does not throw a c++ exception.  Instead, you get an access violation just as you would with a c-array.  

This trade-off is done by design.   I would speculate based that it was a compromise to encourage C users to adopt the c++ containers which they would probably not be inclined to do if it was significantly slower.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question