首先說一下忍宋,java 10 現在已經支持 var 關鍵字推斷 饥脑,也有了repl jshell纠俭,相信 jdk 11 會更出色
重寫 自定義 java 的異常Exception類其實比較簡單
首先創(chuàng)建一個類惊暴,后綴以EXception結尾 比如 NoFoundException ,使人方便識別
然后是重寫構造方法夸浅,然后方法體調用 Exception的super 分別實現仑最,分別是
1.無參數 的,方法體 super()
2.有String message帆喇,方法體 super(String message)
3.有 Throwable cause 警医,方法體 super(Throwable cause)
- 有 String message 和Throwable cause ,方法體 super(String message 坯钦,Throwable cause)
5.有 String message,Throwable cause,boolean enableSuppression,boolean writeableStatckTrace预皇,方法體
super(message,cause,enableSuppression,writeableStatckTrace);
實例:
package com.zhanghn.utils;
public class NoFoundException extends Exception {
public NoFoundException(){
super("file not found");
}
public NoFoundException(String message){
super(message);
}
public NoFoundException(String message,Throwable cause){
super(message,cause);
}
public NoFoundException(Throwable cause){
super(cause);
}
public NoFoundException(String message,Throwable cause,boolean enableSuppression,boolean writeableStatckTrace){
super(message,cause,enableSuppression,writeableStatckTrace);
}
}