5장 예외, 단정, 로깅

public class Main {
	public static void main(String[] args) {
		throwRuntimeException();	// subclasses of RuntimeException: unchecked exceptions
						// doesn't have to be checked
		try {
			throwException(); 	// subclasses of Exception: checked exceptions
		} catch(Exception e) {		// must be handled

		}
	}

	private static void throwRuntimeException() {
		throw new RuntimeException();
	}

	private static void throwException() throws Exception {
		throw new Exception();
	}
}

예외 잡기

try {
	문장
} catch (예외클래스 ex) {
	핸들러
}

지정한 클래스의 예외가 일어나면 제어가 핸들러로 이동한다.

try-with-resources

try (리소스타입1 res1 = init1; 리소스타입2 res2 = init2; ...) {
	문장
}

각 리소스는 반드시 AutoCloseable 인터페이스를 구현하는 클래스에 속해야 한다.
AutoCloseable은 close 메서드 하나만 선언되어있다.

try 블록이 끝날 때 모든 리소스 객체의 close 메서드가 호출된다.

스택 추적

미처리 예외 (uncaught exception) 이 있으면 스택 추적(stack trace)에 표시된다. stack trace는 예외가 던져진 지점에서 대기 중인 모든 메서드 호출의 목록이다.

로깅

소개하는 로거 java.util.logging.Logger aka JUL

참고 문서
Is Standard Java Logging Dead? Log4j vs Log4j2 vs Logback vs java.util.logging

Logging facade : slf4j, commons-logging
Logging engines : log4j, log4j2, logback, JUL

Logging levels

slf4j와 JUL이 함께 사용될 때
FINEST -> TRACE
FINER -> DEBUG
FINE -> DEBUG
INFO -> INFO
WARNING -> WARN
SEVERE -> ERROR


조사한 github의 인기 자바 프로젝트들 중 4.4%만 JUL의 log level을 사용하는 로깅 구문들이 사용됨.

좋은 웹페이지 즐겨찾기