21

In a C program, I generally use EXIT_SUCCESS or EXIT_FAILURE in exit() function to improve clarity and understandability of the program.

But in System.exit() I couldn't use these MACROS.

I can define my own interface as

public interface ReturnValues {
  public int EXIT_SUCCESS = 0;
  public int EXIT_FAILURE = 1;
}

Other than my own implementation, is there any other way in java to use these Macros? (like using predefined library class variables or by implementing predefined interface etc..)

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77
  • 2
    @McDowell yes really it is. ref: http://www.gnu.org/s/hello/manual/libc/Exit-Status.html. Thanks for your comment. I corrected it. But in general theory of any function in C, error will return -1. Am I right? needed explanation. – Muthu Ganapathy Nathan Aug 14 '11 at 14:14
  • 1
    exit codes ranges are [system dependent](http://stackoverflow.com/questions/179565) but I believe POSIX systems restrict the values to 0-255. – McDowell Aug 14 '11 at 19:54
  • @EAGER_STUDENT In 2's complement, -1 maps to a binary number of all 1s. Or, in hex, all Fs. It may not be standard to use as an erroneous exit value, but the difference between 0000 and FFFF is definitely easier to see than the difference between 0000 and 0001. Of course, any sane processor will take the same amount of time telling the difference, but as a human it's nice. – Parthian Shot Aug 08 '14 at 15:04

3 Answers3

8

No, there is no predefined constants in Java for SUCCESS and FAILURE. Certainly because there might be several different kinds of failures, depending on each specific application.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 3
    Just to clarify, no matter which operating system and programming language used, there could be different kinds of failures and failure codes. EXIT_SUCCESS is defined as a successful run, all other codes are failures. EXIT_FAILURE is a generic failure code to be used if you don't want to be specific. The definition of EXIT_SUCCESS is system dependent. In Posix it's 0. Windows defines it as 1. http://www.gnu.org/software/libc/manual/html_node/Exit-Status.html – Robert Jul 17 '12 at 11:12
  • 4
    Downvoted because you're wrong on multiple counts. EXIT_SUCCESS and EXIT_FAILURE exist so that, should you not want to be specific but you _don't know what numbers map to success and failure on the host system_ you can use those constants. Usually, 0 is success and any nonzero value is failure, but that's not universally true, hence the C stdlib provision. Incidentally, for specific error codes (in Linux at least) you can use sysexits.h as a guide. Exit codes vary both by system and application, hence the oddity in Java not providing a standardized interface. – Parthian Shot Aug 08 '14 at 15:00
6

There are no macros in Java. System.exit(0) and System.exit(-1) are plenty readable without getting overly complicated.

Perception
  • 79,279
  • 19
  • 185
  • 195
2

In java, use enums for this kind of thing:

public enum ReturnValue {
    SUCCESS(0),
    FAILURE(-1);

    private int returnCode;

    private ReturnValue(int returnCode) {
        this.returnCode = returnCode;
    }

    public int getReturnCode() {
        return returnCode;
    }
}

To use:

System.exit(ReturnCode.FAILURE.getReturnCode());
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 10
    he asked "`other that my own implementation`". This is one's own implementation. I believe he asks if Java has these built-in constant enums – Yanick Rochon Aug 14 '11 at 11:26
  • 1
    What does it add over plain int constants? – JB Nizet Aug 14 '11 at 11:27
  • @JB: Type checking if you require it at some point somewhere else in your application. – Vivien Barousse Aug 14 '11 at 11:29
  • Yes, of course, but unless this is true, I would just use plain int constants. – JB Nizet Aug 14 '11 at 11:40
  • 3
    Just because Java has `enum` types doesn't mean you should always use them. In this case, you end up writing extra code to define the `enum` and extra code to use it. Not an improvement IMO. – Stephen C Aug 14 '11 at 13:05
  • 2
    At least on Linux, FAILURE is 1 and not -1. But that's the whole point of ISO-C providing EXIT_FAILURE - there is no single constant that should be used on all platforms. It is a shame that JAVA doesn't. – Guenther Brunthaler Mar 05 '19 at 14:23