메뉴 바로가기 검색 및 카테고리 바로가기

한빛미디어

뇌를 자극하는 java 프로그래밍

뇌를 자극하는 java 프로그래밍 질의응답 게시판입니다.

생성자가 발생하는 익셉션(Exercise 5-4)

2013-01-10

|

by stvking

|

Exercise 5-4.png

1745

class Account {
        String accountNo;        //계좌번호
        String ownerName;        //예금주 이름
        int balance;                //잔액
        Account(String accountNo, String ownerName, int balance) throws Exception {
                if (balance < 0)
                        throw new Exception("객체를 생성할 수 없습니다.");
                this.accountNo = accountNo;
                this.ownerName = ownerName;
                this.balance = balance;
        }
        void deposit(int amount) {
                balance += amount;
        }
        int withdraw(int amount) throws Exception {
                if (balance < amount)
                        throw new Exception("잔액이 부족합니다.");                //익셉션을 발생
                balance -= amount;
                return amount;
        }
}

위 클래스의 객체를 생성하는 프로그램을 작성하는 것이었습니다.
그래서 저는 아래 프로그램을 작성했죠.

class MethodExample5 {
        public static void main(String args[]) {
                try {
                Account obj1 = new Account("777-777-777777", "최대박", 1000000);
                System.out.println("잔액 :" + obj1.balance);
                Account obj2 = new Account("888-8888-8888880", "최쪽박", -2000);
                System.out.println("잔액 : " + obj2.balance);
                }
                
                catch (Exception r) {
                        String msg = r.getMessage();
                        System.out.println(msg);
                }
                
                try {
                        int amount = obj1.withdraw(100000);
                        System.out.println("인출액: " + amount);
                }
                catch (Exception e){
                        String msg = e.getMessage();
                        System.out.println(msg);
                }
        }
}

그랬더니 에러메세지가 떳습니다.
에러메세지를 첨부파일로 부쳤습니다.

에러메세지 내용을 해석해주시고, 해결에 필요한 조언을 구하고자 글을 올립니다.
댓글 입력