Sumyungi 0 Newbie Poster

Is there a way in C++ to handle unexpected errors? Other languanges I have learned tend to allow this but for some reason I cannot get the try/catch solution to work in all cases. Take the following example:

int main(){

     vector<int> v;

     try{

          //next line should cause an assertion failure.
          cout << v[1];

     } catch (...) {

          cout << "An error has occurred.";

     }

     return 0;
}

In this example, executing the bad line of code within the try block is useless. Is there a better way?

I understand that it is bad practice to mask errors which should be fixed, but the issue is that I am developing a re-useable user interface which I want to include its own error handling classes and methods to handle code which will be integrated with it at a later date. Obviously I have no guarantee that the code which will be integrated into my own will be error free, so I would like to have some functions to provide useful data to help the user determine where the error is.

Help is much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.