Java

쉽게 배우는 JAVA 2-6(예외 변수 e)

꽃달린감나무 2021. 8. 19. 15:45
728x90

"이 글은 글쓴이가 부스트 코스의 쉽게 배우는 JAVA2를 통해 배운 점을 적는 공간입니다."

 

변수 e

e는 하나의 인스턴스로 다양한 기능을 사용할 수 있다. 

public class ExceptionClass {

	public static void main(String[] args) {
		
		System.out.println(1);
		
		int[] scores = {10,20,30};
		
		try {
		System.out.println(2);
	//	System.out.println(scores[3]);
		System.out.println(3);  //실행되지 않는 코드 try 아래전부
		System.out.println(2/0);
		System.out.println(4);
		} catch(ArithmeticException e) {
			System.out.println("ArithmeticException" + e.getMessage());
			e.printStackTrace();
		} catch(Exception e) {
			System.out.println("Exception");
		}  
		System.out.println(5);

	}

}

위에서 e.getMessge(), e.printStackTrace()를 통해서 예외상황에 대한 정보를 얻을 수 있다.

더 많은 e에 대한 기능을 알아볼려면 아래 링크를 참고하기를 바란다.

https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#constructor.summary

 

Throwable (Java Platform SE 8 )

Provides programmatic access to the stack trace information printed by printStackTrace(). Returns an array of stack trace elements, each representing one stack frame. The zeroth element of the array (assuming the array's length is non-zero) represents the

docs.oracle.com

 

728x90