Ex01_exception
package com.mywork.ex;
import java.util.Scanner;
public class Ex01_exception {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1, num2;
try{
System.out.print("정수1 입력 >> ");
num1 = scanner.nextInt();
System.out.print("정수2 입력 >> ");
num2 = scanner.nextInt();
System.out.println("덧셈 : " + (num1 + num2));
System.out.println("뺄셈 : " + (num1 - num2));
System.out.println("곱셈 : " + (num1 * num2));
System.out.println("나눗셈 : " + (num1 / num2));
} catch (ArithmeticException e) {
System.out.println("0으로 나눌 수 없습니다.");
} finally {
if(scanner != null) scanner.close();
}
}
}
Ex02_exception
package com.mywork.ex;
public class Ex02_exception {
public static void main(String[] args) {
int[] arr = new int[3];
try {
arr[1] = 1;
arr[2] = 2;
for(int n : arr) {
System.out.println(n);
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("인덱스의 가용 범위를 벗어났습니다.");
}
}
}
Ex03_exception
package com.mywork.ex;
public class Ex03_exception {
public static void main(String[] args) {
char gender;
try {
gender = (int)(args[0].charAt(7)) % 2 == 1 ? '남' : '여';
System.out.println(gender);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Run - Run Configuration 메뉴를 통해 주민등록번호를 전달하세요.");
}
}
}
Ex04_exception
package com.mywork.ex;
public class Ex04_exception {
public static void main(String[] args) {
try {
String name = "alice";
System.out.println("이름 : " + name.toString());
} catch (Exception e) {
System.out.println("예외 발생!");
System.out.println(e.getMessage());
System.out.println("----------------");
e.printStackTrace();
}
}
}
Ex05_exception
package com.mywork.ex;
interface Animal{
void walk();
}
class Dog implements Animal{
@Override
public void walk() {
System.out.println("산책한다.");
}
public void sleepTogether() {
System.out.println("같이 자자.");
}
}
class Crocodile implements Animal{
@Override
public void walk() {
System.out.println("악어 피해 도망가자.");
}
}
public class Ex05_exception {
public static void verifyDog(Animal animal) {
try {
((Dog)animal).sleepTogether();
} catch (ClassCastException e) {
System.out.println("Dog 가 아니라서 함께 잘 수 없다.");
System.out.println("예외 발생!!");
System.out.println(e.getMessage());
System.out.println("--------------");
e.printStackTrace();
}
}
public static void main(String[] args) {
verifyDog(new Dog());
verifyDog(new Crocodile());
}
}
Ex06_exception
package com.mywork.ex;
public class Ex06_exception {
public static void divide(int num1, int num2) throws ArithmeticException {
System.out.println("몫 : " + (num1 / num2));
System.out.println("나머지 : " + (num1 % num2));
}
public static void main(String[] args) {
try {
divide(5,2);
divide(5,0);
} catch (ArithmeticException e) {
System.out.println("0으로 나눌 수 없다.");
e.printStackTrace();
}
}
}
Ex07_exception
package com.mywork.ex;
public class Ex07_exception {
public static void divide(int num1, int num2) throws ArithmeticException {
System.out.println("몫 : " + (num1 / num2));
System.out.println("나머지 : " + (num1 % num2));
}
public static void main(String[] args) throws ArithmeticException {
divide(5, 2);
divide(5, 0);
}
}
Ex08_exception
package com.mywork.ex;
class MyException extends Exception{
private static final long serialVersionUID = 1L;
public MyException(String message) {
super(message);
}
}
public class Ex08_exception {
public static void main(String[] args) {
try {
throw new MyException("내가 만든 예외!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Ex09_BankAccount
package com.mywork.ex;
class BankAccount {
private String no;
private long balance;
public BankAccount(String no, long balance) {
this.no = no;
this.balance = balance;
}
public void deposit(long money) throws DepositException {
if (money < 0) {
throw new DepositException(1000, "0보다 작으면 입금 불가!");
}
balance += money;
}
public void withdraw(long money) throws WithdrawException {
if (money > balance) {
throw new WithdrawException(2000, "잔액보다 크면 출금 불가!");
} else if (money < 0) {
throw new WithdrawException(2001, "0보다 작으면 출금 불가!");
}
balance -= money;
}
public void inquiry() {
System.out.println("계좌번호 : " + no);
System.out.println("계좌잔액 : " + balance);
}
public void transfer(BankAccount account, long money) throws DepositException, WithdrawException {
withdraw(money);
account.deposit(money);
}
}
class DepositException extends Exception {
private static final long serialVersionUID = 1L;
private int errCode;
public DepositException(int errCode, String message) {
super(message);
this.errCode = errCode;
}
public int getErrCode() {
return errCode;
}
}
class WithdrawException extends Exception {
private static final long serialVersionUID = 1L;
private int errCode;
public WithdrawException(int errCode, String message) {
super(message);
this.errCode = errCode;
}
public int getErrCode() {
return errCode;
}
}
public class Ex09_BankAccount {
public static void main(String[] args) {
BankAccount parent = new BankAccount("123-456", 10000);
BankAccount child = new BankAccount("654-321", 0);
try {
parent.deposit(10000);
parent.inquiry();
parent.withdraw(10000);
parent.inquiry();
parent.transfer(child, 5000);
parent.inquiry();
child.inquiry();
} catch (DepositException e) {
System.out.println("에러 메시지 : " + e.getMessage() + "(코드 : " + e.getErrCode() + ")");
} catch (WithdrawException e) {
System.out.println("에러 메시지 : " + e.getMessage() + "(코드 : " + e.getErrCode() + ")");
}
}
}
Ex10_BankAccount
package com.mywork.ex;
class BankAccount2 {
private String no;
private long balance;
public BankAccount2(String no, long balance) {
this.no = no;
this.balance = balance;
}
public void deposit(long money) throws DepositException2 {
if (money < 0) {
throw new DepositException2(1000, "0보다 작으면 입금 불가!");
}
balance += money;
}
public void withdraw(long money) throws WithdrawException2 {
if (money > balance) {
throw new WithdrawException2(2000, "잔액보다 크면 출금 불가!");
} else if (money < 0) {
throw new WithdrawException2(2001, "0보다 작으면 출금 불가!");
}
balance -= money;
}
public void inquiry() {
System.out.println("계좌번호 : " + no);
System.out.println("계좌잔액 : " + balance);
}
public void transfer(BankAccount2 account, long money) throws DepositException2, WithdrawException2 {
withdraw(money);
account.deposit(money);
}
}
class BankAccountException extends Exception {
private static final long serialVersionUID = 1L;
private int errCode;
public BankAccountException(int errCode, String message) {
super(message);
this.errCode = errCode;
}
public int getErrCode() {
return errCode;
}
}
class DepositException2 extends BankAccountException {
private static final long serialVersionUID = 1L;
public DepositException2(int errCode, String message) {
super(errCode, message);
}
}
class WithdrawException2 extends BankAccountException {
private static final long serialVersionUID = 1L;
public WithdrawException2(int errCode, String message) {
super(errCode, message);
}
}
public class Ex10_BankAccount {
public static void main(String[] args) {
BankAccount2 parent = new BankAccount2("123-456", 10000);
BankAccount2 child = new BankAccount2("654-321", 0);
try {
parent.deposit(10000);
parent.inquiry();
parent.withdraw(10000);
parent.inquiry();
parent.transfer(child, 5000);
parent.inquiry();
child.inquiry();
} catch (BankAccountException e) {
System.out.println("에러 메시지 : " + e.getMessage() + "(코드 : " + e.getErrCode() + ")");
}
}
}