Custom exception class uses an enumeration and

Custom exception

Understanding error, RuntimeException (run-time, virtual machine fault report) and non RuntimeException (compile time, the compiler to find fault)
    in 1.java abnormalities are inherited from Throwable, that there are two important subclasses of error and exception directly .
    2.java error error, mostly by the virtual machine to the burst error, the wrong program can not handle, such as OutOfMemoryError, when the JVM needs more memory space are not met, it will burst OutOfMemoryError.
    3.Exception, abnormal, under which many classify as abnormal and can not check to check abnormal, abnormal and non-runtime exceptions, basic concepts, like running, but saying nothing different. Which have an important subclass RuntimeException runtime exceptions i.e., other direct property of non RuntimeException subclass, such as IOException, SQLException like.
        a. Non-RuntimeException when the code is written, the compiler to prompt you to check that you want to try catch or throws treatment.
        b.RuntimeException, the compiler will not help you automatically check, when you run the program, the virtual machine will give you a burst of errors for you to deal with, this is often the logic of our coding or non-standard due to


Custom exception, step three steps :
    1. Inheritance a RuntimeException
    2. overloaded constructor
    3. override methods
such as:
public class SellException the extends a RuntimeException {
1 inherited a RuntimeException
    Private code Integer; // exception code, similar to the status code front end processing according to the code for the corresponding
    public SellException (resultEnum resultEnum) {2. overloaded constructor
        Super (resultEnum.getMessage ());
        this.code resultEnum.getCode = ();
}
     public SellException (code Integer, String Message) {
        Super (Message);
        this.code = code;
}
}
usage: throw new SellException (ResultEnum.PRODUCT_NOT_EXSIT);

Enum constant object encapsulates

Enumeration enum type object,
the steps :
    1. Create a constructor parameter and a constant value Constant Meaning
    2. Create enumeration object
@Getter
public enum {OrderStatusEnum the implements CodeEnum
    NEW (0, "new order"),
    the FINISHED (. 1, "end"),
    the cANCEL (2, "canceled"),
;
    Private Integer code;
    Private String the Message;

    OrderStatusEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
}

Guess you like

Origin www.cnblogs.com/h-c-g/p/11225034.html