자바 기초

Throws

coding1842 2024. 1. 29. 20:03
package com.study.javastudy.study;

public class ExceptionExam2 {
    public static void main(String[] args) {
        int i = 10;
        int j = 0;
        try {
            int k = divide(i, j);
            System.out.println(k);
        } catch (ArithmeticException e) {
            System.out.println(e.toString()); // java.lang.ArithmeticException: / by zero
        }
    }

    public static int divide(int i, int j)  throws ArithmeticException{
        // throws는 ArithmeticException 예외가 발생하니 그쪽에다 처리하도록 명령한다
        int k = i/j;
        return k;
    }
}