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

  • Home
  • SEARCH
  • 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 1110095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:20:31+00:00 2026-05-17T02:20:31+00:00

I saw the following snippet code: class Foo { public: void virtual func() throw

  • 0

I saw the following snippet code:

class Foo
{
public:
        void virtual func() throw (int, float) = 0;
};

class Bar : public Foo
{
public:
        void virtual func() throw(short);      // line 1: compile error "
                                                                      // looser throw specifier"
        void virtual func() throw();                // line 2: can compile
        void virtual func() throw(float, int); // line 3: can compile
        void virtual func() throw(float);        // line 4: can compile
        void virtual func() throw(int);           // line 5: can compile

};

int main(void)
{
        return 1;
}

Q1> What is meaning of

void virtual func() throw (int, float) = 0;

Q2> why line1 cannot pass the compiler?

Thank you

  • 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-17T02:20:32+00:00Added an answer on May 17, 2026 at 2:20 am

    Let’s break this down. The declaration:

    void virtual func() throw (int, float) = 0;
    

    has 2 constructs that you’re asking about. the =0 construct tells the compiler that the declared function is ‘abstract’, which tells the compiler that the function need not be defined in the class Foo (though it can be – but it usually isn’t) and that an object of class Foo cannot be directly created – either as a local, global or via new. However, you can have pointers or references to objects of class Foo. Some derived class needs to override the function as a non-abstract function – objects of that class can be directly created (as long as there are no other abstract functions that haven’t been made ‘concrete’).

    The throw (int, float) construct is an exception specifification. This tells the compiler that the function’s contract is that it will only throw exceptions of type int or float if it throws an exception. If the function throws some other kind of exception, the compiler is obligated to handle that specially (by calling std::unexpected()).

    Now, if you try to override that function in a derived class with the following declaration:

    void virtual func() throw(short);
    

    You’re saying that the function’s contract is that it will throw exceptions of type short if an exception is thrown. However, throwing short is not part of the contract of the function being overridden, so the compiler doesn’t permit it.

    If you declare the override like so:

    void virtual func() throw(float);
    

    You’re saying that the override can throw a float, which is part of the contract of the original declaration (if it never throws an int that doesn’t break the contract – the original contract only says that the function is allowed to throw an int, not that it has to).

    The relevant part of the standard is 15.4/3 Exception specifications:

    If a virtual function has an
    exception-specification, all
    declarations, including the
    definition, of any function that
    overrides that virtual function in any
    derived class shall only allow
    exceptions that are allowed by the
    exception-specification of the base
    class virtual function.

    Note that the standard explicitly states that an exception specification is not part of the function’s type (15.4/12), so a function pointer can, for example, point to functions that have different exception specifications.

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

Sidebar

Related Questions

I saw the following code snippet: class WindowHandle { public: WindowHandle(WINDOW_HANDLE handle) : w(handle)
Recently I saw the following code that creates a class in javascript: var Model.Foo
I saw the following program on the internet public class Test1{ public static void
I saw the following code: class NullClass { public: template<class T> operator T*() const
I just saw someone post the following, in a rant on ugly code: public
I saw the following code snippet: <?php if(!empty($_POST)): // case I: what is the
This morning I accidentally saw the following snippet code, I was fairly surprised because
Going through K&R I saw the following code snippet of a function, strcopy, which
I saw the following class declaration in vb.net: Public Class [Interface] Implements TestInterface What
I saw the following code, public override bool Equals(object obj) { // From the

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.