국비 22일차 throw, 예제

2021. 2. 3. 18:16

2021.02.03 국비교육 22일차

Throw

  • 명시적 예외를 만듦
package com.test;

public class Ex09_7 {

    public static void check(int num) throws Exception{ // 발생한 ex를 호출한 곳으로 위임
        if (num < 100) {
            throw new Exception ("num값이 100보다 작다"); // throw Exception의 강제 발생, 생성자에 넣은 문자열은 message가 됨
        }
    }

    public static void check2(int num) throws Exception{
        if(num > 100) {
            throw new Exception("num값이 100보다 크다");
        }
    }

    public static void main(String[] args){
        System.out.println("시작");

        try {
            check(70);
        } catch (Exception e) {
            System.out.println("예외발생" + e.getMessage());
        }

        try {
            check2(110);
        } catch (Exception e) {
            System.out.println("예외발생" + e.getMessage());
        }

        System.out.println("종료");

    }

}

/*
시작
예외발생num값이 100보다 작다
예외발생num값이 100보다 크다
종료
*/
  • 오늘 수업은 주로 workshop과 기타 예제관련한 내용으로 진행됨 추후에 정리하여 올릴 예정

+ Recent posts