標籤:sys check bsp trace 異常 exception user cep system
類1:public class LogicException extends RuntimeException {
//商務邏輯異常
/**
*
* @param message 異常資訊
*/
public LogicException(String message) {
super(message);
}
/**
*
* @param message 異常資訊
* @param cause 當前異常的根本原因
*/
public LogicException(String message, Throwable cause) {
super(message, cause);
}
}
類2:private static String[] arr={"張三","小張","小明","李四"};
public static void main(String[] args) {
try {
checkUsername("張三");
} catch (LogicException e) {
String errorMsg=e.getMessage();
System.out.println("給使用者看"+errorMsg);
}
}
private static Boolean checkUsername(String username){
for (String name : arr) {
if(name.equals(username)){
throw new LogicException("很抱歉,"+username+"已經註冊了!");
}
}
System.out.println("註冊成功!");
return true;
}
}
[總結] 1.自訂異常: class 異常類名 extends Exception { public 異常類名(String msg) { super(msg); } } 2.標識可能拋出的異常: throws 異常類名1,異常類名2 3.捕獲異常: try{} catch(異常類名 y){} catch(異常類名 y){} 4.方法解釋 getMessage() //輸出異常的資訊 printStackTrace() //輸出導致異常更為詳細的資訊
Java 自訂異常類