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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:42:15+00:00 2026-05-30T22:42:15+00:00

Consider the following horribleness: #include <iostream> struct thing { thing() { std::cout << thing

  • 0

Consider the following horribleness:

#include <iostream>

struct thing
{
    thing()
    { 
        std::cout << "thing ctor\n";
        throw 5; 
    }
};

void SomeTerminateHandler()
{
    std::cout << "Uncaught exception!?\n";
}

int IHopeThisGetsCalledFirst()
{
    std::cout << "IHopeThisGetsCalledFirst()\n";
    std::set_terminate(SomeTerminateHandler);
    return 2;
}

const int x = IHopeThisGetsCalledFirst();
const thing y;

int main()
{
}

Output:

IHopeThisGetsCalledFirst()
thing ctor
Uncaught exception!?

This relies on static initialization order (which I can mess with, for MSVS anyway) so it’s not ideal even if the following questions have acceptable answers.

  • What can I do in SomeTerminateHandler to ‘catch’ the exception so I can display an error dialog box or log out the details of the error?
  • abort is called after SomeTerminateHandler in the above program. Why? This says “The terminate handler by default calls cstdlib’s abort function.” – but I’m not using the default terminate handler, and mine doesn’t call abort.

Is there anything I can do to handle exceptions in static initialization time. If not, why not? Why isn’t there something in the language to allow this?

  • 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-30T22:42:17+00:00Added an answer on May 30, 2026 at 10:42 pm

    What can I do in SomeTerminateHandler to ‘catch’ the exception so I can display an error dialog box or log out the details of the error?

    Nothing – only exception handlers can access the thrown exception.

    abort is called after SomeTerminateHandler in the above program. Why?

    Your terminate handler must not return; the standard (C++11, 18.8.3.1) requires that it “shall terminate execution of the program without returning to the caller” – the page you link to also says the same thing. If you break that requirement, then anything could happen.

    Is there anything I can do to handle exceptions in static initialization time?

    You can catch the exception in the constructor; or you can avoid complex static objects.

    If not, why not? Why isn’t there something in the language to allow this?

    I can’t answer why, but C++ doesn’t handle complex static objects at all well. Even if your compiler does have non-standard extensions to specify the initialisation order, I suggest avoiding them if at all possible.

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

Sidebar

Related Questions

Consider following source files 1.cpp #include <iostream> using namespace std; struct X { X()
Consider following code: main.cpp: #include <iostream> typedef void ( * fncptr)(void); extern void externalfunc(void);
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Consider following example: #include <iostream> #include <functional> #include <algorithm> #include <vector> #include <boost/bind.hpp> const
Consider following codes: #include <stdio.h> #include <malloc.h> void allocateMatrix(int **m, int l, int c)
Consider following example. #include <iostream> #include <boost/optional.hpp> template < typename A > int boo(
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };

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.