
Exception (Java Platform SE 8 )
| checked | unchecked | |
|---|---|---|
| 처리 여부 | 반드시 예외 처리해야 함, 안하면 컴파일 에러 발생 | 명시적인 처리를 강제하지 않음 |
| 확인 시점 | 컴파일 단계 | 실행단계(런타임 단계) |
| 예외 발생 시 트랜잭션 처리 | 자바의 경우 롤백 처리 없음, 스프링은 있음 | 자바의 경우 롤백 처리 없음, 스프링은 있음 |
| 대표 예시 | Exception의 상속받는 하위클래스 중 runtime exception을 제외한 모든 예외 (대표적으로 IOException) | runtimeException 하위 예외 (대표적으로 NullPointerException) |
try {
//예외가 발생할 일이 없는 로직 A
//예외가 발생할 수도 있는 로직 B
//예외 발생할 일이 없는 로직 C
} catch(Exception 클래스 e) {
//위 로직 B에서 예외가 발생하면 여기서 예외처리
}
-> B로직에서 예외가 발생했을 경우, C 로직은 실행이 안됨. 그래서 finally가 존재 아니면 try-catch 문 밖에
db에서 가져온 값이 null일 때, npe가 발생하지는 않지만 우리가 의도한 리턴값이 아니기 때문에 처리해줘야 함. → 객체가 null이면 exception이 발생하지 않는데 null 일 땐 무슨 값을 반환??? → throw
개발자가 임의로 발생시키는 예외, 자바 입장에서는 예외가 아니지만 개발자가 예외로 처리하고자 할 때 사용
위 예시처럼 우리가 찾으려는 객체가 null 값이면 자바의 입장에선 예외가 아니지만 개발자가 의도한 값을 반환하지 않으므로 임의로 예외 처리를 해줄 때 사용한다.
예시코드)
public Product getProduct(int id) {
Product gotProduct = productTable.get(id);
try {
if(gotProduct != null)
return productTable.get(id);
else
throw new NoSuchElementException("제품이 없습니다.");
} catch (NoSuchElementException e) {
return new NullProduct();
}
}
try에서 throw를 던져주면 catch 해서 예외처리에 쓸 객체를 반환한다.
에외가 터지만 호출자에게 예외를 던져버리는 것
그러면 호출자에서 try-catch로 에러를 잡아서 처리
[Service]
public Product getProduct(int id) {
try {
return productRepository.getProduct(id);
} catch (NoSuchElementException e) {
return new NullProduct();
}
}
[Repository]
public Product getProduct(int id) throws NoSuchElementException{
Product gotProduct = productTable.get(id);
if(gotProduct == null) throw new NoSuchElementException("제품이 없습니다.");
return productTable.get(id);
}
unchecked와 checked
throws 키워드를 사용하여 예외를 선언해야 합니다throws 키워드를 사용하여 예외를 선언할 필요가 없습니다. 호출하는 쪽에서는 이 예외를 처리하지 않아도 컴파일 오류가 발생하지 않습니다.public class ProductNotFoundException extends NoSuchElementException {
private String message;
public ProductNotFoundException(String message) {
this.message = message;
}
@Override
public String getMessage() {
return message;
}
}
예외가 발생할 때 어디서 예외처리를 해줘야 할까?