Solved

try... catch problem

Posted on 2011-02-17
8
430 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
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 143

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
Technology Partners: 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!

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

756 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