JAVA - Newlecture (4)

suesue lee·2022년 3월 8일
0

JAVA

목록 보기
5/6

이 글은 Newlecture의 자바 예외처리 강의를 보며 정리한 글입니다.

🤍 예외 처리에서 예외란 무엇을 말하는가? 🤍

오류의 종류

🤍 예외를 처리한다는 것은 무엇을 말하는 무엇을 말하는 것이고 어디에서 처리해야하는가? 🤍

오류처리의 주체

Librarty를 통한 보고

자바에서 예외를 보고(통지)하는 값과 명령어

①번에서 오류가 발생하면 throw 1; 코드를 작성하여 오류를 처리한다.
throw x; 를 할 경우 문제발생하는 이유 : 성적관리 프로그램에서 'x'의 의미를 인식하지 못한다.

직관적으로 이해할 수 있게끔 특수한 객체를 사용한다.

throw new 권한없음예외(); : 오류 처리 과정에서는 이를 '권한예외가 발생했다.'라고 표현한다. (오류 처리에서 이해가능)
코드 상에서도 구현코드를 이해하기가 쉽고 유지·보수가 쉬워진다.

예외 처리 방법

🤍 예외를 던지는 클래스 준비하기 🤍

예외를 보고하는 계산기 클래스 라이브러리 만들기
ExceoptionPrj Package 추가 → Calculator.java / Program.java 생성

Program.java

public class Program { 
	public statouic void main (String [] args) {
    	Calculatro clac = new Calculaor();
        
        int result = 0;
        result = Calculator.add(3,4); // 덧셈을 할 때마다 값을 넘겨주는 방식
        result = Calculator.sub(3,4);
        result = Calculator.multi(3,4);
        result = Calculator.div(3,4);
    }
}

Calculator.java

public class Calculaor {

	public Calculator() {
		// 기본 생성자
    }
    
    // Program.java의 Calculator.add(3,4); 에 대한 코드
    
    public static void add (int x, int y) {
    	return x+y;
    }
    public static int sub (int x, int y) {
    	return x-y;
    }
    public static int multi (int x, int y) {
    	return x*y;
    }
    public static int div (int x, int y) {
    	retrun x/y;
    }
}

🤍 예외 클래스 생성과 던지기(throw) 🤍

Program.java

public class Program { 
	public statouic void main (String [] args) throws 천을_넘는_예외, 음수가되는예외 {
    	Calculatro clac = new Calculaor();
        
        int result = 0;
        result = Calculator.add(3,-4); // 덧셈을 할 때마다 값을 넘겨주는 방식
        System.out.printf("add : %d\n", result);
        result = Calculator.sub(3,4);
        System.out.printf("sub : %d\n", result);
        result = Calculator.multi(3,4);
        System.out.printf("multi : %d\n", result);
        result = Calculator.div(3,4);
        System.out.printf("div : %d\n", result);
        
    }
}

Calculator.java

public class Calculaor {

	public Calculator() {
		// 기본 생성자
    }
    
    // Program.java의 Calculator.add(3,4); 에 대한 코드
    
    public static void add (int x, int y) throws 천을_넘는_예외, 음수가되는예외 {
    	
        int result = x + y;
        if(result > 1000)
        	throw new 천을_넘는_예외();
        
        if(result  < 0) 
        	throw new 음수가되는예외();
    
    	return x+y;
    }
    public static int sub (int x, int y) {
    	return x-y;
    }
    public static int multi (int x, int y) {
    	return x*y;
    }
    public static int div (int x, int y) {
    	retrun x/y;
    }
}

❕ throw new 라는 명령어를 사용했을 때 Superclass가 자동으로 Exception으로 선택된다. ❕

천을넘는예외.java

public class 천을_넘는_예외 extends Exception {

}

음수가되는예외.java

public class 음수가되는예외 extends Exception {

}

🤍 예외 처리하기 🤍

Program.java

public class Program { 
	public statouic void main (String [] args) throws 음수가되는예외 {
    	Calculatro clac = new Calculaor();
        
        int result = 0;
        try {
        	result = Calculator.add(3,10004);
        }
        catch (천을_넘는_예외 e) { 
        	System.out.println(e.getMessage());
        }
        System.out.printf("add : %d\n", result);
        result = Calculator.sub(3,4);
        System.out.printf("sub : %d\n", result);
        result = Calculator.multi(3,4);
        System.out.printf("multi : %d\n", result);
        result = Calculator.div(3,4);
        System.out.printf("div : %d\n", result);
        
    }
}

천을넘는예외.java

public class 천을_넘는_예외 extends Exception {
	@Override
    public String getMessage() {
    	return "입력 값의 합이 1000을 넘는 오류가 발생하였습니다.";
    }
}

🤍 다중 예외처리 🤍

Program.java

public class Program { 
	public statouic void main (String [] args) {
    	Calculatro clac = new Calculaor();
        
        int result = 0;
        try {
        	result = Calculator.add(3,4);
            System.out.printf("add : %d\n", result);
        	result = Calculator.sub(3,4);
        }
        catch (천을_넘는_예외 e) { 
        	System.out.println("특화된 처리");
        	
        }
        catch (Exception e) {
      	 	System.out.println("음수 처리");
        }
        finally { // 모든 예외의 catch 블럭들이 동일한 로직을 담고 있다면, 그것을 집중화 할 수 있는 공간이 finally 블럭이다.
        	System.out.println("입력 값에 오류가 있습니다.");
        }

        System.out.printf("sub : %d\n", result);
        result = Calculator.multi(3,4);
        System.out.printf("multi : %d\n", result);
        result = Calculator.div(3,4);
        System.out.printf("div : %d\n", result);
        
    }
}

Calculator.java

public class Calculaor {

	public Calculator() {
		// 기본 생성자
    }
    
    // Program.java의 Calculator.add(3,4); 에 대한 코드
    
    public static void add (int x, int y) throws 천을_넘는_예외, 음수가되는예외 {
    	
        int result = x + y;
        
        if(result > 1000)
        	throw new 천을_넘는_예외();
        
        if(result  < 0) 
        	throw new 음수가되는예외();
    
    	return result;
    }
    public static int sub (int x, int y) throw 음수가되는예외 {
 
        int result = x - y;
        
        if(result  < 0) 
        	throw new 음수가되는예외();
    
    	return result;
    }
    public static int multi (int x, int y) {
    	return x*y;
    }
    public static int div (int x, int y) {
    	retrun x/y;
    }
}

🤍 Un-checked 예외 🤍

Calculator.java

public class Calculaor {

	public Calculator() {
	
    }
    
    public static void add (int x, int y) {
    	
        int result = x + y;
        
        if(result > 1000)
        	throw new 천을_넘는_예외();
        
        if(result  < 0) 
        	throw new 음수가되는예외();
    
    	return result;
    }
    public static int sub (int x, int y) {
 
        int result = x - y;
        
        if(result  < 0) 
        	throw new 음수가되는예외();
    
    	return result;
    }
    public static int multi (int x, int y) {
    	return x*y;
    }
    public static int div (int x, int y) {
    	retrun x/y;
    }
}

Program.java

public class Program { 
	public statouic void main (String [] args) {
    	Calculatro clac = new Calculaor();
        
        int result = 0;
    
        result = Calculator.add(3,4);
        System.out.printf("add : %d\n", result);
        result = Calculator.sub(3,4);
      
        System.out.printf("sub : %d\n", result);
        result = Calculator.multi(3,4);
        System.out.printf("multi : %d\n", result);
        result = Calculator.div(3,0);
        System.out.printf("div : %d\n", result);
    }
}

천을넘는예외.java

public class 천을_넘는_예외 extends RuntimeException {
	@Override
    public String getMessage() {
    	return "입력 값의 합이 1000을 넘는 오류가 발생하였습니다.";
    }
}

음수가되는예외.java

public class 음수가되는예외 extends RuntimeException {

}

출처 : 링크텍스트

profile
미래의 웹프로그래머

0개의 댓글