Solved

try... catch problem

Posted on 2011-02-17
8
426 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 74

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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

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

panJames
0
 
LVL 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now