Solved

try... catch problem

Posted on 2011-02-17
8
428 Views
Last Modified: 2012-05-11
Hello Experts!

Please have a look at the code below.

I expect to catch exception, instead I get exception risen by vector code and it crashes my application.

What do I do wrong here?

Thank you

panJames

#include <vector>
#include <iostream>
#include <exception>

using namespace std;

void main()
{
	int a;
	std::vector<int> abc;

	abc.push_back(1);
	abc.push_back(2);

	try
	{
		a = abc[2];
	}
	catch (exception& e)
	{
		cout << e.what();

	}

}

Open in new window

0
Comment
Question by:panJames
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34917482
you need to put the try larger:
#include <vector>
#include <iostream>
#include <exception>

using namespace std;

void main()
{
        try
        {
        int a;
        std::vector<int> abc;

        abc.push_back(1);
        abc.push_back(2);

                a = abc[2];
        }
        catch (exception& e)
        {
                cout << e.what();

        }

}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34917533
To further angelIII's comment, you only reserved the space--you didn't actually initialize it, so you are trying to call a method on a null object, hence the exception.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 34917618
The operator[] for std::vector doesn't throw an exception, so you can't expect to catch it. If you want to get an exception, use the at method :
try
	{
		a = abc.at(2);
	}
	catch (exception& e)
	{
		cout << e.what();
	}

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:panJames
ID: 34917653
@angelIII:

1. Why do I need larger try{} selection? Thought that I need to make my try{} where I expect it to crash...
2. It does not help anyway. Program never comes to line:

 cout << e.what();

0
 

Author Comment

by:panJames
ID: 34917661
@kaufmed: I know it.

panJames
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34917724
>>  @kaufmed: I know it.


Uhhh...   then you have your answer  = )

Calling a method on an uninstantiated object raises an exception.
0
 

Author Comment

by:panJames
ID: 34917757
@Infinity08:

I am confused now.

My understanding of try{} was that if anything wrong happens inside try{} then exception is thrown and all I need to do is to catch it (like in Delphi).

So C++ program does not have such functionality?

How can I catch something simple like:


int a = 12;
int b = 0;
int c;


try
{
 c = a / b;
}


panJames
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34917781
>> My understanding of try{} was that if anything wrong happens inside try{} then exception is thrown and all I need to do is to catch it (like in Delphi).

No, not anything. Anything that throws a C++ exception will be caught if you have an appropriate catch handler. But divisions by 0 or segmentation faults eg. won't.


>> So C++ program does not have such functionality?

Some compilers might add it as an extension, but it's not generally available no.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

777 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