자바 Exception !!!

자바 Exception !!!

작성일 2008.02.24댓글 1건
    게시물 수정 , 삭제는 로그인 필요

아 하다가하다가 답답해서 올려요 =_=

 

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

profile_image 익명 작성일 -

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());
  try {
   account.withdraw(10.0);
  } catch (OverdraftException e) {
      e.printStackTrace();
  }
  System.out.println(account.getBalance());
 }

}

 

 

public void withdraw(double amount) throws OverdraftException

// 저 throws OverdraftException 이라는 예외를 함수를 호출하는 쪽에서 처리하겠다는 선언입니다.

// 따라서 이 withdraw 함수를 호출하는 함수는 무조건 OverdraftException 을 try~catch로 처리하던가

// 그 처리를 다시 미루는 throws OverdraftException 하는 방법이 있습니다.

//처리를 하지 않는다면 컴파일시 에러가 나게 되지요~

{
  if (balance < amount) {
   throw new OverdraftException("잔액이 부족합니다.", amount - balance);
  } else {
   balance -= amount;
  }
 }

 

두번째 방법

 

class ControlBank {
 public static void main(String[] args) throws OverdraftException{
  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());
 }

}

이렇게 다시 처리를 호출하는 쪽 즉 JVM 쪽으로 미뤄버립니다.~ 가장 안좋은 코딩 방법입니다.

항상 예외가 발생할 경우 적절한 처리를 해주어야 합니다. JVM에 미루거나 에러 메세지만 띄우는

코딩 습관은 나중에 유지 보수를 어렵게 할뿐더러 코드의 이해도도 떨어뜨립니다.

 

 

자바 exception.printStackTrace

... exception.printStackTrace(pw); pw.close(); %> 에러예제 파일이 없습니다! 내용 : <%= exception.getMessage() %> 책보면서 공부중인데 exception....

자바 exception 질문드립니다

class MyException extends Exception{ public MyException... catch (Exception e) { System.out.println(e.getMessage... catch문에서의 Exception 존재 - Exception...

자바 Exception in thread "main...

디버그를 해봤더니 21번째 라인에서 질문과 같이 Exception in thread "main" java.... NullPoint Exception이 뜨는거죠 가장 편한건 for 문 바로 밑에 students[i] = new...

자바 exception 관련 질문입니다.

... exception오류가 생기면 오류 부분을... close(); } catch (FileNotFoundException exception )... "); } catch (IOException exception ) { System.out....

자바 Exception 관련 질문입니다.

... 다름이 아니고 자바소스에서 계속 에러가 발생하여... close(); } catch(Exception exception1) { } try { b64os.close(); } catch(Exception exception2) { } try...