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
Solved

try... catch problem

Posted on 2011-02-17
8
429 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 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
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.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
c++ reading data from file into two dimensional array 3 117
outlook office 365 8 144
Why  my code (program) build with old compiler? 11 76
Outlook 13 41
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

856 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