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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:01:10+00:00 2026-05-27T19:01:10+00:00

I am using mingw g++ 4.6.1 with -O0, WinXP SP2. Minimal working example is

  • 0

I am using mingw g++ 4.6.1 with -O0, WinXP SP2.

Minimal working example is here.

g++ is configured with –disable-sjlj-exceptions –with-dwarf2.

GetLastError() returns 0 or 2 depeding on how the exception is thrown:

throw runtime_error(error_message());

bogus “error code: 0” is printed, and

const string msg = error_message();

throw runtime_error(msg);

prints “error code: 2” as expected.

First, I thought GetLastError() is invoked twice but debugging shows it is invoked exactly once, as expected.

What is going on?

  • 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-27T19:01:10+00:00Added an answer on May 27, 2026 at 7:01 pm

    It’s possible that the code that sets up a throw calls a Win32 API function inside itself somewhere, that resets the Last-Error value to 0. This may be happening before your call to error_message().

    Calling GetLastError() does not automatically reset the Last-Error value to 0, so it is safe to call twice.

    Whether your compiler/runtime generates code that calls a Win32 API function will be up to your specific runtime. In order to be safe and not depend on this, use the two-statement version:

    const string msg = error_message();
    throw runtime_error(msg);
    

    Better yet, for future readers of your code it would be useful to call GetLastError() outside error_message():

    const string msg = error_message(GetLastError());
    throw runtime_error(msg);
    

    This way, readers will see the GetLastError() call immediately after the corresponding Win32 API call, where it belongs.

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

Sidebar

Related Questions

I'm working on a C++ library using mingw. However, as soon as I perform
I've been using Code::Blocks with MinGW on a WinXP box for a while now.
I am working on learning the windows API and am using mingw as my
I'm using MinGW with GCC 3.4.5 (mingw-special vista r3). My C application uses a
I am trying to use native windows API with Qt using mingw toolset. There
I am trying to learn the wxWidgets library, using MinGW and msys to compile
Is there possibility to compile windows service using only mingw c++ compiler and library?
I want to make my C++ project cross platform, and I'm considering using Cygwin/MinGW.
I am using C++ from Mingw, which is the windows version of GNC C++.
I am using CodeBlocks and MinGW toolchain which is essentially GCC. I was using

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.