ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 210423_1(예외_Exception)
    JAVA(Sol)(정리대기중..언젠가) 2021. 4. 23. 10:09

    예외일 경우는 컴파일까지는 정상적으로 처리 되며, 실행시 실행 조건에 따라 오류

     

    try {

          여러가지 예외 사항 지점 감쌈

         }

    catch(소) {처리1}

    catch(중) {처리2}

          ...

    catch(대   Excaption e) {처리3}

     

    catch 블럭이 여러개인 경우, 예외 발생에 따라서 처리를 달리하고자 할때 사용

    나중에 나오는  catch문에 큰범위의 예외 처리 클래스를 넣는다.

    package chapter15;
    //처리를 나누어서 하는 경우
    class ExceptionError1 {
    	public static void main(String[] args) {
    		try {
    			System.out.println("매개변수로 받은 두개의 값");
    			int a = Integer.parseInt(args[0]);
    			int b = Integer.parseInt(args[1]);
    			System.out.println("a=" + a +"b=" + b );
    			System.out.println("a를 b로 나눈 몫=" +(a/b));
    			System.out.println("나눗셈이 원활히 수행되었습니다.");
    		}
    		catch(ArithmeticException e) { // catch가 여러개- 여러상황에따라 다르게처리
    			System.out.println("==========================");
    			System.out.println("ArithmeticException 처리 루틴");
    			System.out.println(e+"예외발생");
    			
    		}
    		catch(ArrayIndexOutOfBoundsException e) {
    			System.out.println("==========================");
    			System.out.println("ArrayIndexOutOfBoundsException 처리루틴");
    			System.out.println(e+"예외발생");
    		}
    		catch(NumberFormatException e) {
    			System.out.println("==========================");
    			System.out.println("NumberFormatException 처리루틴");
    			System.out.println(e+"예외발생");
    		}
    		catch(Exception e) { //이외모든 예외를 잡아냄
    			System.out.println("===========================");
    			System.out.println("알수없는 문제가 발생했습니다.");
    			
    		}
    		finally {
    			System.out.println("===========================");
    			System.out.println("예외 처리를 끝내고 finally 블럭을 수행합니다");
    			
    		}
    		System.out.println("나머지 모듈 정상 작동!!!");
    
    	}
    
    }
    
    //a=10; b=2;
    //매개변수로 받은 두개의 값
    //a=10b=2
    //a를 b로 나눈 몫=5
    //나눗셈이 원활히 수행되었습니다.
    //===========================
    //예외 처리를 끝내고 finally 블럭을 수행합니다
    //나머지 모듈 정상 작동!!!
    
    //a=10; b=0;
    //매개변수로 받은 두개의 값
    //a=10b=0
    //==========================
    //ArithmeticException 처리 루틴
    //java.lang.ArithmeticException: / by zero예외발생
    //===========================
    //예외 처리를 끝내고 finally 블럭을 수행합니다
    //나머지 모듈 정상 작동!!!
    
    //a=10; b;
    //매개변수로 받은 두개의 값
    //==========================
    //ArrayIndexOutOfBoundsException 처리루틴
    //java.lang.ArrayIndexOutOfBoundsException: 1예외발생
    //===========================
    //예외 처리를 끝내고 finally 블럭을 수행합니다
    //나머지 모듈 정상 작동!!!
    
    //a=열; b=둘;
    //매개변수로 받은 두개의 값
    //==========================
    //NumberFormatException 처리루틴
    //java.lang.NumberFormatException: For input string: "열"예외발생
    //===========================
    //예외 처리를 끝내고 finally 블럭을 수행합니다
    //나머지 모듈 정상 작동!!!
    
    

     


    예외처리를 하고싶은데, 어떤 예외 클래스를 사용할지 모르겠다면 - Excpetion 클래스

    가장 큰 예외클래스로 처리합니다. 분수대원리(?) - 위(작은)에서 놓친 것 아래(큰)서 캐치가능 

     Exception클래스가 가장 큰 예외클래스이다.

     

    java.lang.Object  - 최상위 클래스

     

       java.lang.Throwable - 예외+오류일때 최상위 <---어쩌다한번나옴~~

          java.lang.Exception - 예외 일때 최상위  <--가장많이 사용함

             java.lang.RuntimeException - -실행

                  java.lang.ArithmeticException

    package chapter15;
    
    class ExceptionError2 {
    
    	public static void main(String[] args) {
    		
    		try {
    			System.out.println("매개변수로 받은 두 개의 값");
    			int a = Integer.parseInt(args[0]);
    			int b = Integer.parseInt(args[1]);
    			System.out.println("a =" + a + "b=" + b);
    			System.out.println("a를 b로 나눈 몫=" + (a / b));
    			System.out.println("나눗셈이 원활히 수행되었습니다");
    
    		} catch (Exception e) {
    			System.out.println("========================");
    			System.out.println(e.toString());
    			
    		} finally {
    			System.out.println("========================");
    			System.out.println("예외처리를 끝내고 finally 블럭을 수행합니다.");
    
    		}
    
    		System.out.println("나머지 루틴을 정상 처리합니다");
    	}
    
    }
    //매개변수로 받은 두 개의 값
    //========================
    //java.lang.ArrayIndexOutOfBoundsException: 0
    //========================
    //예외처리를 끝내고 finally 블럭을 수행합니다.
    //나머지 루틴을 정상 처리합니다
    

     


    사용자 정의의 예외처리 클래스

     관리를 해야 하는 클래스는 여러개 분산되어 있는 경우

     사용자 정의의 예외 클래스를 별도로 구현하여 집중화 시켜서 관리할수있다. --개발자 실사용

    package chapter15;
    
    // 사용자 정의의 예외처리
    class UserException { // 예외처리 클래스 생성
    	public UserException() {
    
    	}
    
    	public UserException(String message) {
    		System.out.println("파일에 에러 내역을 기록합니다...");
    		System.out.println("에러원인 : " + message);
    	}
    }
    
    class ExceptionError3 {
    	public static void main(String[] args) {
    
    		try {
    			System.out.println("매개변수로 받은 두 개의 값");
    			int a = Integer.parseInt(args[0]);
    			int b = Integer.parseInt(args[1]);
    			System.out.println("a=" + a + "b=" + b);
    			System.out.println("a를 b로 나눈 몫=" + (a / b));
    			System.out.println("나눗셈이 원활히 수행되었습니다");
    
    		} catch (Exception e) {
    			new UserException(e.toString());
    
    		} finally {
    			System.out.println("=========================");
    			System.out.println("예외처리를 끝내고 finally 블럭을 수행합니다.");
    
    		}
    		System.out.println("나머지 루틴을 정상처리합니다");
    
    	}
    
    }
    
    //매개변수로 받은 두 개의 값
    //파일에 에러 내역을 기록합니다...
    //에러원인 : java.lang.ArrayIndexOutOfBoundsException: 0
    //=========================
    //예외처리를 끝내고 finally 블럭을 수행합니다.
    //나머지 루틴을 정상처리합니다
    

Designed by Tistory.