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 8898861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:43:06+00:00 2026-06-15T00:43:06+00:00

Why doesn’t this piece of code run without error? Isn’t bad_exception supposed to be

  • 0

Why doesn’t this piece of code run without error? Isn’t bad_exception supposed to be called automatically when an unexpected exception is encountered? Or is it necessary to set a handler for it?

#include <iostream>
#include <exception>
using namespace std;
class C{};
void test() throw(bad_exception)
{
    throw C();
}
int main()
{
    try
    {
         test();
    } catch(bad_exception& e)
    {
        cout << "Caught ";
    }
}
  • 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-06-15T00:43:07+00:00Added an answer on June 15, 2026 at 12:43 am

    In C++03 theory:
    If you throw an exception that is not in the exception specification, unexpected() gets called. If you have not set the unexpected handler via set_unexpected() this means terminate() gets called which is the case you observed. If you have set an unexpected handler that does not call terminate but throws an exception, and if that exception is not listed in your exception specification, it gets translated into bad_exception. So in order to get the expected result, call set_unexpected() first with an appropriate handler.

    In C++03 practice:
    Some compilers do not support exception specifications at all (other than throw()), others just don’t evaluate/check them for correctness. As Herb Sutter points out, exception specifications create a clumsy “shadow type system” that is not easy to handle right (if it is possible at all). Therefore..

    … in C++11:
    Exception specifications are deprecated. You should rather not use them. However, there is a nothrow operator that has a slightly different functionality than throw()


    PS: so why have std::bad_exception in C++03?
    You hav three different regions of code:

    • The function you are writing an exception specification for.
    • The “foreign” code you are calling from that function that might or might not throw an exception that does not match you specification.
    • The (maybe also unknown) unexpected handler that can be anything and should either terminate/exit/abort the program or throw anything.

    So if the “foreign” code throws an exception that violates your exception specification, you have three possible outcomes of that:

    1. The handler terminates the program. There is not much you can do about that unless you set your own handler in the function.
    2. The handler throws an exception that matches your exception specification. And all is well.
    3. The handler throws something else. What do you want the runtime to do now? That’s where bad_exception comes in: If it is in your specification, the “something else” gets translated into a bad_exception, and the program continues. If it’s not, terminate gets called.

    Setting your own handler inside the function disables any handler that was previously set by anyone else who just wants to use your function. He will not expect you to disable his handler. Besides, the handler is meant as a global what-happens-if policy and thus is nothing you should care about in a single function implementation.

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

Sidebar

Related Questions

Why doesn't this code work? b if b = true Error: undefined local variable
Doesn't seem to be anything about this online, I need to run fetch query
Why doesn't this code work? I'am using FF. <head> <script type=text/javascript> document.getElementById(someID).onclick = function(){
Why doesn't this code print operator=? #include <iostream> using namespace std; class A{ public:
This doesn't work: int number = 1; String numberstring = IntToString(number); I get The
Why doesn't this work? Some Usercontrol (name: myUserControl) with an image control (name: myImage):
Why doesn't this test case work? <?php // cards with cyrillic inidices and suits
Doesn't matter what I do, I simply can't get this to play a sound
This doesn't make sense to me yet. So the first while loop runs and
Why doesn't this work? type RetryBuilder(max) = member x.Return(a) = a // Enable 'return'

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.