Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unable to catch STL acess violation exceptions in VS2005

Posted on 2007-03-22
4
Medium Priority
?
473 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

704 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