Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:45:25+00:00 2026-05-30T09:45:25+00:00

In C++ you may declare function with exception specification like this: int foo() const

  • 0

In C++ you may declare function with exception specification like this:

int foo() const throw(Exception);

I found those two links:

  • http://www.cplusplus.com/doc/tutorial/exceptions/ and
  • http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr156.htm

But several things end up unanswered…

Question 1: why to add exception specification? Will it bring any performance increase? What will be different for compiler? Because it seems just like an information for programmer to me.

Question 2: what will happend (what should happen) if I throw something that isn’t in specification? For example:

int foo() throw(int) {
        throw char; // Totally unrelated classes, not types in real
}

Question 3: function/method shouldn’t throw anything. I found at least two (three, alternative syntax for different compilers) ways to specify no exception throwing:

  • int foo() throw();
  • int foo() __attribute(nothrow)__ for gcc
  • int foo() nothrow for visual C++

Which one is “correct”? Is there any difference? Which one should I use?

Question 4: “standart exceptions”, bad_alloc,bad_cast,bad_exception,bad_typeid and ios_base::failure.

Ok bad_alloc is self explaining and I know how (and more importantly when) to use it (add to exception specification), but what about the others? None of them does really ring a bell… Which “code pieces” are they associated with? Like bad_alloc is associated with new char[500000].

Question 5: If I have exception classes hierarchy, like this:

    class ExceptionFileType {
             virtual const char * getError() const = 0;
    };

    class ExceptionFileTypeMissing : public ExceptionFileType {
            virtual const char *getError() cosnt {
                    return "Missing file";
            }
    }

Should I use:

    int foo() throw(ExceptionFileType);

Or:

    int foo() throw(ExceptionFileTypeMissing,ExceptionFileTypeNotWritable,ExceptionFileTypeNotReadable,...)

Note: answers with references would be great. I’m looking for good practice tips.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:45:26+00:00Added an answer on May 30, 2026 at 9:45 am

    The simple “good practice” tip is: don’t use exception specifications.

    Essentially the only exception to that is the possibility of an empty exception specification: throw(). That’s sufficiently useful that in C++11 it’s been given its own keyword (noexcept). It’s generally agreed that any non-empty exception specification is a lousy idea though.

    Exception specifications (other than noexcept) are officially deprecated — and unlike many deprecated features, removing this would affect little enough source code that I think there’s a good chance it really will eventually be removed (certainly no guarantee, but a pretty fair chance anyway).

    As for what happens when/if you do throw an exception of a type not allowed by the exception specification: std::unexpected() gets invoked. By default, that invokes terminate(). You can use std::set_unexpected to set your own handler — but about all you can reasonably do is add some logging before you terminate(). Your unexpected handler is not allowed to return.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ActionScript 3 you can declare optional parameters like this: function (i:int = 0,
Can I declare a non-member function (global function, may be) as const in C++?
I understand that this may be construed as one of those what's your preference
I have function like this : CREATE OR REPLACE FUNCTION f_insert_account(usernames character varying, passwds
I realise that this may seem like a stupid request but I'm going to
It seems to me there are other functional dependencies you may wish to declare
In my C# source code I may have declared integers as: int i =
may i know how to use this getMicrophone with simpleFLVWriter to create flv with
I recently tried to create an object like this: var carousel = { $slider:
I have next class and try to declare member function which will return pointer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.