Question

I saw a big amount of questions about System.exit(int status) on stackoverflow.

For example:

Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java

difference between System.exit(0) and System.exit(-1)

But I do not see answer on my partial question:

Do we have any direct connection between exit status and System Error Codes for Microsoft products(Microsoft site)?

Sure, we do not have any direct connections, but: can we make this connection?

If we run our java program on Windows OS and our java program has System.exit(int status) statement, can we use System Error Codes list or no?

Is it good idea or not?

What do you think about it? Any other ideas?

Example:

  • Java program was run via command line on Wisdows OS;
  • Our java program catched exception like FileNotFoundException;
  • After this we made decision to close our program via System.exit(int state);
  • In this case can exit status be equal 2?

because(Microsoft site):

ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified. 
Était-ce utile?

La solution

The exit status is a value of your choice. If you call your program and then react based on the exit value returned after it has finished execution, you are free to handle them as you wish.

There is only one general rule, which is that exit status 0 assumes that your program completed without error. Furthermore a negative exit status is usually assumed to be a termination because of an unexpected error, while a positive exit status is assumed to be a (more or less) graceful termination.

But again: if there is no batch file calling your code, no one will ever react on the exit status, and therefore you can return whatever you want, it will have no effect.

Autres conseils

The exit code is simply informational and can be based on your design. The Microsoft codes are important because MS software may try to recognize them, likewise on Linux there is a different set of predefined agreed-upon codes. It won't hurt to use one of the sets.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top