[JAVA]/11.예외처리

[강의] 예외클래스 종류

76beny 2022. 5. 29. 20:53

 1. 산술예외
ArithmeticException

int a = 10 / 0;

 

2.배열범위를 뛰어넘는 
 ArrayIndexOutOfBoundsException

int[] array = new int[10];
array[12] = 10;

오류이유 : 공간은 처음에 10개로 생성했는데 밑에서 12로 받아서 오류

 

3.음수예외
NegativeArraySizeException 

array = new int[-1];

 

4.null예외
 NullPointerException 

array = null;
array[10] = 10;

오류이유: null인 상태에서 접근하려고 하니까 오류!

 

5. 형변환예외
ClassCastException 

Object obj = new Object();
String str = (String)obj;

오브젝트로 선언했는데 스트링으로 받아서 오류

 

 

// Run time error 아님.--------------------------------------------------
		// System Error // OutOfMemoryError : Required array length too large
		//스텍에서 에러가 넘침
		String str = "11248901284092187409281490128490";
		while(true) {
			str += str;
		}
		
		//java.lang.OutOfMemoryError: Java heap space
		//힙에서 에러가 넘침
		StringBuffer sb = new StringBuffer();
		while(true) {
			sb.append(str);
		}
		
		// java.lang.StackOverflowError
		method(); // 무한루프//본인이 본인을 또 호출함!
	}
	public static void method() {
		int temp = 0;;
		method();
	}