자바 Exception !!!
-
게시물 수정 , 삭제는 로그인 필요
아 하다가하다가 답답해서 올려요 =_=
Account.java
public class Account{
protected double balance;
public Account(double init_balance){
balance = init_balance;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
public void withdraw(double amount) throws OverdraftException{
if( balance < amount){
throw new OverdraftException("잔액이 부족합니다.", amount-balance);
}
else{
balance -= amount;
}
}
}
OverdraftException.java 파일
public class OverdraftException extends Exception{
private double deficit;
public OverdraftException(String message, double deficit){
super(message);
this.deficit = deficit;
}
public double getDeficit(){
return deficit;
}
}
ControlBank.java 파일
class ControlBank{
public static void main(String[] args){
Account account = new Account(100.0);
System.out.println(account.getBalance());
account.deposit(100.0);
System.out.println(account.getBalance());
account.withdraw(10.0);
System.out.println(account.getBalance());
}
}
위에 두개는 컴파일 에러 안나는데 마지막 파일에서 컴파일 에러나내요;
ControlBank.java 7 : unreported exception OverdraftException; must be caught or declared to be thrown
account.withdraw(10.0);
이렇게 나오는데요;
이거 왜이러는지 모르겠어요 ㅠㅠ;
내공 20 이요~;;
아 하다가하다가 답답해서 올려요 =_=
Account.java
public class Account{
protected double balance;
public Account(double init_balance){
balance = init_balance;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
balance += amount;
}
public void withdraw(double amount) throws OverdraftException{
if( balance < amount){
throw new OverdraftException("잔액이 부족합니다.", amount-balance);
}
else{
balance -= amount;
}
}
}
OverdraftException.java 파일
public class OverdraftException extends Exception{
private double deficit;
public OverdraftException(String message, double deficit){
super(message);
this.deficit = deficit;
}
public double getDeficit(){
return deficit;
}
}
ControlBank.java 파일
class ControlBank{
public static void main(String[] args){
Account account = new Account(100.0);
System.out.println(account.getBalance());
account.deposit(100.0);
System.out.println(account.getBalance());
account.withdraw(10.0);
System.out.println(account.getBalance());
}
}
위에 두개는 컴파일 에러 안나는데 마지막 파일에서 컴파일 에러나내요;
ControlBank.java 7 : unreported exception OverdraftException; must be caught or declared to be thrown
account.withdraw(10.0);
이렇게 나오는데요;
이거 왜이러는지 모르겠어요 ㅠㅠ;
내공 20 이요~;;
#자바 exception #자바 exception 종류 #자바 exception 발생시키기 #자바 exception 만들기 #자바 exception in thread main #자바 exception e #자바 exception 클래스 #자바 exception throw #자바 exception 무시 #자바 exception in thread main java.lang.error unresolved compilation problem