class HungryException extends Exception {
public HungryException(String msg) {
super(msg);
}
}
class NoPowerException extends Exception {
public NoPowerException(String msg) {
super(msg); //Exception(메시지) super(메시지) -> detailMessage = 메시지;
}
}
public static int energy() throws Exception {
int n = -2;
// return -1; 힘이없다.
if (n == -1) {
throw new NoPowerException("힘이 없다");
}
if (n == -2) {
throw new HungryException("배고프다");
}
// return -2; 배고프다.
return 8; // 0~10 가진 에너지
}
try {
int energy = energy();
System.out.println("남은에너지:" + energy);
} catch (NoPowerException e) {
System.out.println("NoPowerException 인식 완료");
System.out.println(e.getMessage());
} catch (HungryException e) {
System.out.println("HungryException 인식 완료");
System.out.println(e.getMessage());
} catch (Exception e) {
// System.out.println("문제가 발생함.");
System.out.println(e.getMessage());
}
-HashMap : 동일한 키값이 들어오면 덮어쓰기가 된다.