Thread: Fatal Error Handling

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    Fatal Error Handling

    I'm normally a POSIX programmer but I'm using the Windows API for this project, which is the first time that I've ever used it.

    I'm kind of confused about what's the correct and/or best way to handle errors. I'm sorry if this is a newbie question but I'm unable to find a straight answer.

    From MSDN's page on the WinMain() function;
    Return Value

    If the function succeeds, terminating when it receives a WM_QUIT message, it should return the exit value contained in that message's wParam parameter. If the function terminates before entering the message loop, it should return zero.
    In the POSIX world, when a program runs into a fatal error (out of memory, or it is unable open a necessary file, being a couple of examples), it would return a non-zero error code back to the operating system.

    According to the above, I should return zero. I've look at many examples/tutorials that do the same. The documentation for WM_QUIT and PostQuitMessage() also don't mention what to return on error. Does anyone know what the correct procedure is for this?

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    MSDN requires some getting used to.

    Quote Originally Posted by MSDN
    If the function succeeds, terminating when it receives a WM_QUIT message, it should return the exit value contained in that message's wParam parameter.
    If you get a WM_QUIT msg, the wParam element of the msg struct will contain the value you should return.

    EDIT: This return depends on your requirements, mostly the wParam contains zero (because the application exited gracefully). Often this is done by default handlers / code.
    If your app fails and the OS closes it, sending the WM_QUIT (and also the error code).

    If you catch the error and call for a WM_QUIT, you can set what suits your design.

    Quote Originally Posted by MSDN
    If the function terminates before entering the message loop, it should return zero.
    If, for any reason, the application terminates before starting the message pump you should return zero (fails before calling Get/PeekMessage()).

    This is a special case and possibly has some connection to the purging of msgs from the OS queue after the app closes.
    Last edited by novacain; 12-16-2009 at 04:36 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fatal error in fprintf
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 10-14-2005, 05:56 AM
  2. Borland C++BuilderX, fatal error
    By beezm in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2005, 05:45 AM
  3. Fatal errors
    By cheeisme123 in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2003, 10:03 PM
  4. fatal exception 0E
    By juhigarg in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2001, 04:40 AM
  5. Fatal error :/
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-13-2001, 08:53 PM