반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

디케이

Throws 예외처리(try, catch, finally) 본문

Java

Throws 예외처리(try, catch, finally)

디케이형 2020. 11. 24. 10:23
반응형

throws

throws는 예외가 발생했을때 예외를 호출한 쪽에서 처리하도록 던져준다.

    public class ExceptionExam2 {   
        public static void main(String[] args) {
            int i = 10;
            int j = 0;
            int k = divide(i, j);
            System.out.println(k);
        }

        public static int divide(int i, int j){
            int k = i / j;
            return k;
        }
    }

정수를 매개변수로 2개를 받아들인 후 나눗셈을 한 후 그 결과를 반환하는 divide메소드
main메소드에서는 divde메소드를 호출

다음과 같이 divide메소드를 수정

    public class ExceptionExam2 {

        public static void main(String[] args) {
            int i = 10;
            int j = 0;
            int k = divide(i, j);
            System.out.println(k);
        }

        public static int divide(int i, int j) throws ArithmeticException{
            int k = i / j;
            return k;
        }
    }

메소드 선언 뒤에 throws ArithmeticException 이 적혀있는 것을 알 수 있습니다. 이렇게 적어놓으면 divide메소드는 ArithmeticException이 발생하니 divide메소드를 호출하는 쪽에서 오류를 처리하라는 뜻입니다.

    package javaStudy;
    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("0으로 나눌수 없습니다.");
            }

        }

        public static int divide(int i, int j) throws ArithmeticException{
            int k = i / j;
            return k;
        }

    }
반응형

'Java' 카테고리의 다른 글

2004.2.3 연월일 입력받아 2004.02.03으로 출력  (0) 2020.11.26
자바: float 소수점 자릿수 정하기  (0) 2020.11.26
Exception 예외처리(try, catch, finally)  (0) 2020.11.24
익명클래스  (0) 2020.11.24
내부 클래스  (0) 2020.11.24