1. AOBE[예외발생]?

  1. [설명]

    1. 초기에 정해진 배열의 크기보다 더 많은 양의 데이터를 넣고자 할 때 주로 발생합니다
  2. [예제]

    // Array out bound exception 발생
    public class main {
    	
    	public static void main(String[] args) {
    			int[] arr = new int[5];
    
    			for(int i = 0; i <= 5; i++) {
    						arr[i] = i;
    		}
    	}
    }
    // 에러나는 이유?
    /*
    	arr의 범위는 0 ~ 4 총 5개의 인덱스를 가지고 있습니다
    	arr[5]는 존재하지 않기 때문에 인덱스 범위를 초과한 것입니다
    	그래서 ArrayIndexOutBoundsException이 발생합니다
    */
    

2. AOBE[예외처리]?

  1. [설명]

    1. 예외발생 시 처리방법은 try/catch구문을 사용해서 해결합니다
    2. try에 예외가 발생할 것 같은 구간을 try { … }에 담아주고, catch(발생할 것 같은 예외, 예외 변수명) { 예외 발생시 실행될 코드 };를 작성해주면 됩니다
    3. finally의 경우 옵션 코드이기 때문에 생략해도 상관 없습니다!
  2. [예제]

    // Array out bound exception 예외처리
    public class main {
    	
    	public static void main(String[] args) {
    			try {
    						int[] arr = new int[5];
    
    						for(int i = 0; i <= 5; i++) {
    										arr[i] = i;
    					} catch(ArrayIndexOutOfBoundsException e) {
    							System.out.println("-ArrayIndexOutOfBoundsException 발생-");
    							System.out.println("현재 코드를 체크해주세요!");
    					} finally {
    							System.out.println("예외처리 코드가 오류없이 진행 완료되었습니다.");
    					}
    		}
    	}
    }
    

3. ESE[예외발생]?

  1. [설명]

    1. EmptyStackException : stack 에서 공백일때 발생하는 에러 코드입니다
  2. [예제]

    // **EmptyStackException 발생
    
    StackPopException isEmpty = new StackPopException();
    
    isEmpty.push(10);
    isEmpty.push(20);**
    
    System.out.println("pop : " + isEmpty.pop3());
    System.out.println("pop : " + isEmpty.pop3());
    System.out.println("pop : " + isEmpty.pop3());
    
    // **EmptyStackException 결과
    // 10
    // 20
    // EmptyStackException 발생합니다**
    

4. ESE[예외처리]?

  1. [설명]

    1. throow new RuntimeException(”메세지”); 처리를 해줍니다
    2. Exception(”EmptyStackException 발생했습니다”); 로 처리를 할 수 있습니다
  2. [예제]

    // **EmptyStackException 처리
    
    public class main {
    	// throw new RuntimeException(); 을 사용함
    	public int pop() {
    				if(isEmpty()) throw new RuntimeException("스택이 비었습니다.");
    				return this.arr[--pointer];
    		}
    
    }
    
    StackPopException isEmpty = new StackPopException();
    
    isEmpty.push(10);
    isEmpty.push(20);**
    
    System.out.println("pop : " + isEmpty.pop3());
    System.out.println("pop : " + isEmpty.pop3());
    System.out.println("pop : " + isEmpty.pop3());
    
    // **EmptyStackException 결과
    // 20
    // 10
    // Exception in thread "main" 
    // java.lang.RuntimeException: 스택이 비었습니다.**