在java的异常类体系中,Error和RuntimeException是非检查型异常,其他的都是检查型异常。
所有方法都可以在不声明throws的情况下抛出RuntimeException及其子类
不可以在不声明的情况下抛出非RuntimeException
简单的说,非RuntimeException必要自己写catch块处理掉。
RuntimeException不用try catch捕捉将会导致程序运行中断,若用则不会中断
Exception:在程序中必须使用try...catch进行处理。
RuntimeException:可以不使用try...catch进行处理,但是如果有异常产生,则异常将由JVM进行处理。
对于RuntimeException的子类最好也使用异常处理机制。虽然RuntimeException的异常可以不使用try...catch进行处理,但是如果一旦发生异常,则肯定会导致程序中断执行,所以,为了保证程序再出错后依然可以执行,在开发代码时最好使用try...catch的异常处理机制进行处理。
interface BizCode {
val code: Int
val msg: String
}
enum class BizCodes(override val code: Int, override val msg: String): BizCode {
// ====================================================== //
// 公共错误码 0 - 999 //
// ====================================================== //
/**
* 未知错误.
*/
C_0(0, "未知错误"),
/**
* HTTP Request 参数错误.
*/
C_999(999, "HTTP Request 参数错误"),
// ====================================================== //
// client 错误码 1000 - 1999 //
// ====================================================== //
/**
* 未发现指定 client_id 客户端记录.
*/
C_1000(1000, "未发现指定 client_id 客户端记录"),
C_1001(1001, "client_secret 不匹配"),
// ====================================================== //
// user 错误码 2000 - 2999 //
// ====================================================== //
/**
* 未发现指定 email 的用户.
*/
C_2000(2000, "未发现指定 email 的用户"),
C_2011(2011, "password 不匹配"),
//
;
override fun toString(): String {
return "[$code] $msg"
}
}
class BizCodeException : RuntimeException {
val bizCode: BizCode
constructor(bizCode: BizCode) : super(bizCode.toString()) {
this.bizCode = bizCode
}
constructor(bizCode: BizCode, e: Exception) : super(bizCode.toString(), e) {
this.bizCode = bizCode
}
override fun fillInStackTrace() = this
}
public interface IPairs<K, V, C extends Enum> {
/**
* 返回枚举对象
* */
C get();
/**
* 返回枚举项的 key
* */
K key();
/**
* 返回枚举项的 value
* */
V value();
}
public enum StatusEnum implements IPairs<Integer, String, StatusEnum> {
DISABLED(0, "record has been disabled"),
ENABLED(1, "record has been enabled"),
DELETES(9, "record has been deleted")
;
private int code;
private String desc;
StatusEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}
public StatusEnum get() {
return this;
}
@Override
public Integer key() {
return this.code;
}
@Override
public String value() {
return this.desc;
}
}
public enum ResponseEnum implements IPairs<Integer, String, ResponseEnum> {
SUCCESS(10000, "success"),
FAILED(10001, "failed")
;
private int code;
private String desc;
ResponseEnum(int code, String desc) {
this.code = code;
this.desc = desc;
}
public ResponseEnum get() {
return this;
}
@Override
public Integer key() {
return this.code;
}
@Override
public String value() {
return this.desc;
}
}
public class AnswerApp {
public static void main(String[] args) {
invoke(StatusEnum.ENABLED);
System.out.println();
response(ResponseEnum.SUCCESS);
}
private static void response(IPairs pairs) {
System.out.println(pairs.get() == ResponseEnum.SUCCESS);
System.out.println(MessageFormat.format("key: {0}, value: {1}", pairs.key(), pairs.value()));
}
private static void invoke(IPairs pairs) {
System.out.println(pairs.get() == StatusEnum.ENABLED);
System.out.println(MessageFormat.format("key: {0}, value: {1}", pairs.key(), pairs.value()));
}
}
————————————————
https://blog.csdn.net/u010979642/java/article/details/93494983
https://blog.csdn.net/weixin_43604769/article/details/102237447
网友评论