Exception handling in c++

try and catch in c++

Page 1 of 1

6 Replies - 1194 Views - Last Post: 18 August 2010 - 09:27 AM Rate Topic: -----

#1 shahjahan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-October 09

Exception handling in c++

Posted 18 August 2010 - 07:47 AM

HI this is shahjahan i need a help in performing some problems in c ++ as i am btech 2nd year student

here is my question:

what header file or what type of file should be included to run the exception handling so as to enable try,throw,catch blocks ??,thankyo..
Is This A Good Question/Topic? 0
  • +

Replies To: Exception handling in c++

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Exception handling in c++

Posted 18 August 2010 - 07:56 AM

You dont need a header for exception handling? You can include <exception> that includes a base class specifically designed to declare objects to be thrown as exceptions
Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Exception handling in c++

Posted 18 August 2010 - 07:58 AM

View Postshahjahan, on 18 August 2010 - 03:47 PM, said:

HI this is shahjahan i need a help in performing some problems in c ++ as i am btech 2nd year student

here is my question:

what header file or what type of file should be included to run the exception handling so as to enable try,throw,catch blocks ??,thankyo..

Just the standard iostream.

I edited your topic title, because it was not very descriptive to your question.
Next time use more descriptive topic titles.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Six222*


Reputation:

Re: Exception handling in c++

Posted 18 August 2010 - 07:58 AM

None, try, catch and throw are keywords in C++

example:

...

 try
  {
    throw 20;
  }
  catch (int x)
  {
    cout << "An exception occurred. Exception Number: " << x << endl;
  }
...


Was This Post Helpful? 0

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Exception handling in c++

Posted 18 August 2010 - 08:01 AM

I'm guessing the OP is using some 20+-year-old compiler and can't understand why exception handling isn't working. Just a guess though.
Was This Post Helpful? 0
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Exception handling in c++

Posted 18 August 2010 - 08:50 AM

if you want to catch an exception as a string, then you need to include the exception header

eg
#include <exception>

try
{
    if (there was a problem)
		throw std::exception("Some error");
}
catch (std::exception e)
{
   output e.what();
}

Was This Post Helpful? 0
  • +
  • -

#7 Galik  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 23-January 10

Re: Exception handling in c++

Posted 18 August 2010 - 09:27 AM

You don't need any header files but to throw/catch exceptions that are compatible with the standard libraries you can include these:

#include <exception>
#include <stdexcept>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1