Returning Error Codes [C#]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
For starters, I had a tool (App.exe) that was written in C++6, one of the most important aspects was the return codes which indicated where the tool failed.
This was really simple to do, I made my MAIN return int and then when I get to failure points I just do "return 1;" or "return 2;", etc... and this would return the correct value.

Now I am re-writing the same tool in C# (as a Console Application called App.exe) but the Return Codes are not working.
So far no matter what I try it always returns a value of 0x0 no matter where it fails, I have already tried a few different ways (as seen below) and nothing seems to works.

Code:
try
{
	// DO THE WORK
}
catch
{
	//Environment.ExitCode = RET_UNKNOWNSTATIONTYPE;
	//Environment.Exit(RET_UNKNOWNSTATIONTYPE);
	//return RET_UNKNOWNSTATIONTYPE;
}

So from the above code you can see I have already tried 3 different approaches, setting the ExitCode, Exiting with an ExitCode, and simply Returning the value (as I used to do with C++6 and worked fine)
However, in all 3 cases I get the following (from the debug window)
The program '[1704] App.exe' has exited with code 0 (0x0).

No matter what I try it never returns with a return code (note that RET_UNKNOWNSTATIONTYPE = 0x00000001;)
Any clues, help, or hints would be greatly appreciated.
Thanks,
 
I ran into the same problem 2 years back. The return code was set properly w/o a debugger attached. I just used Environment.Exit.
 
I wouldn't trust the output window for the ExitCode. If you test your EXE with a batch job (to test the exit code) or test it otherwise, using the first two examples should work fine. I couldn't get the "return NN;" to work even when Main was declared as returning int.

I've used the "Environment.ExitCode = NN;" syntax and it works fine.

-ner
 
Back
Top