For a process exiting normally in Windows, the exit code of the process is generally either the return value from main
, or the exit code passed to std::exit
. %ERRORLEVEL%
can then be used to query the exit code, and that can be used to determine whether the program executed either correctly, or there were some exceptional inputs/failures that indicate a particular problem (application specific).
However, I'm interested in the exit code if the process crashes. Take a very simple example program:
int main()
{
int * a = nullptr;
*a = 0xBAD;
return 0;
}
When I compile this and run in Windows, on the command line I get:
MyCrashProgram.exe -> crashes
echo %ERRORLEVEL% -> -1073741819
The exit code is consistently this number. Which leads me to several questions:
-1073741819
somehow predicable, based on the invalid write crash? Note, I am not interested in how to modify the program to catch the exception. I am interested in classifying crashes that can happen in existing programs, that I may not be able to modify.
The comment about STATUS_ACCESS_VIOLATION
, led me to the documentation on GetExceptionCode
:
The return value identifies the type of exception. The following table identifies the exception codes that can occur due to common programming errors. These values are defined in WinBase.h and WinNT.h.
EXCEPTION_ACCESS_VIOLATION
maps to STATUS_ACCESS_VIOLATION
in the list that follows. All exceptions in the list prefixed with STATUS
are directly defined to exception codes prefixed with EXCEPTION
. Following the documentation to RaiseException
, it explains the process of attempting to debug the exception when it occurs, the final step being:
If the process is not being debugged, or if the associated debugger does not handle the exception, the system provides default handling based on the exception type. For most exceptions, the default action is to call the ExitProcess function.
So to answer my questions:
EXCEPTION_STATUS_VIOLATION
.