예외처리
예외 처리(Exception Handling) 는 프로그램 실행 중에 발생할 수 있는 예외적인 상황을 적절하게 처리하기 위한 프로그래밍 기법입니다.
Exception 종류
Throwable
├── Error (Not usually caught by applications)
│ ├── LinkageError (Errors related to linking problems with classes)
│ │ ├── NoClassDefFoundError
│ │ └── ExceptionInInitializerError
│ ├── VirtualMachineError (Errors produced by the JVM making continuation impossible)
│ │ ├── OutOfMemoryError
│ │ └── StackOverflowError
│ └── AssertionError (Used for internal assertions in Java; normally not caught)
── Exception (The main exception class for catchable conditions)
├── IOException (Checked: Exceptions that must be caught or declared)
│ ├── FileNotFoundException (File not found)
│ └── EOFException (End of file reached unexpectedly)
├── RuntimeException (Unchecked: Represents programming errors)
│ ├── NullPointerException (Null object usage)
│ ├── IndexOutOfBoundsException (Accessing illegal indexes)
│ │ ├── ArrayIndexOutOfBoundsException (Invalid array index access)
│ │ └── StringIndexOutOfBoundsException (Invalid string index access)
│ ├── IllegalArgumentException (Calling methods with improper arguments)
│ │ ├── NumberFormatException (Failed number conversion)
│ │ └── IllegalThreadStateException (Operations on threads at illegal states)
│ └── ArithmeticException (Errors in arithmetic operations, e.g., division by zero)
└── SQLException (Checked: Errors related to database access)
1. 예외처리 키워드
- throw: 예외를 강제로 발생시키는 키워드입니다.
- throws: 메서드 내에서 예외를 직접 처리하지 않고 호출자에게 예외 처리를 위임한다는 의미입니다.
- try-catch : 던져진 exception을 해당 scope 내에서 코드로 받아서 처리를 진행함.
- try-catch-final : try catch 구문후 마지막으로 finally 안의 구문은 반드시 실행시킴.
- 예외가 발생해서 throws를 한다고해도 마지막 처리를 도와줌. 예를 들면 파일 연결끊기와 같은 자원정리라던지.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("This block is always executed.");
}
2. 예외의 분류
- Checked 예외: 컴파일 단계에서 반드시 처리해야 함을 확인시키는 예외들로, 일반적으로 예상 가능한 예외
- 예: IOException, FileNotFoundException, SQLException
- * 최근에는 checked exception을 선호하지 않고 unchecked excpetion으로 변환하여 throw 하는 추세임
- Unchecked Exception이 선호되는 이유 : Flexibility with less code ; checked excpetion은 선택의 여지가 없이 직접 errohandling를 해야하나, unchecked excpetion은 errorhandling을 할지 안할지 결정할 수 있음. 그리고 handling시에는 별도의 코드복잡성 증가 없이 Global Exception Handler에서 일괄처리할 수 있기때문.
- 예: IOException, FileNotFoundException, SQLException
class Parent {
void method() throws InterruptedException {
// ...
} }
class Child extends Parent {
@Override
void method() throws Exception {
// ...
} }
public class Test {
public static void main(String[] args) {
Parent p = new Child();
try {
p.method();
} catch (InterruptedException e) {
// InterruptedException 처리 하지만 그 외 예외는 놓치게 된다
}
}
- Unchecked 예외: 실행 시점(Runtime)에서 발생하며, 컴파일러가 강제로 예외 처리를 요구하지 않습니다. 주로 프로그램의 버그, 잘못된 입력 등 예측 불가능한 상황에서 발생합니다.
- 예: IllegalArgumentException, IllegalStateException, NullPointerException, IndexOutOfBoundsException, ArithmeticException
- 상속관계에서의 예외처리 - 부모 메서드가 체크 예외를 method에서 throws하지 않는 경우, 재정의된 자식 메서드도 체크 예외를 던질 수 없다. 자식 메서드는 부모 메서드가 던질 수 있는 체크 예외의 하위 타입만 던질 수 있다. 단, unchecked 예외는 무관하기때문에 checked excpetion을 catch한후 unchecked exception을 던지는 식으로 처리한다.
- Java에서 다형성을 이용하여 부모변수로 자식생성자를 받을때, 컴파일러는 부모변수의 구조만 읽을 수 있기때문에, 예외처리의 컴파일 검사를 부모기준으로 적용할 수 밖에없다. 때문에, 자식메소드는 부모의 제약에 따르도록 강제한다.
예외와 에러
- 에러 : 프로그램의 정상적인 실행을 방해하는 심각한 문제로, 애플리케이션 코드에서 복구할 수 없는 상황. 주로 JVM(Java Virtual Machine) 레벨에서 발생. 대부분 시스템 리소스 부족, JVM의 내부 문제로 인해 발생하므로 복구가 불가능.
- OutOfMemoryError: 메모리가 부족할 때.
- StackOverflowError: 재귀 호출이 너무 깊을 때.
- InternalError: JVM 내부 오류.
- 프로그램 실행 중 예상 가능한 문제(예: 잘못된 입력값, 네트워크 연결 문제 등)로 인해 발생. 대부분의 경우, 적절한 **예외 처리(try-catch)**를 통해 복구 가능. 예외를 처리하지 않으면 애플리케이션이 종료될 수 있으므로 예외 처리를 명시적으로 구현.
예외처리기법
- try-catch 예외처리 :
- try{ 예외가 발생할 수 있는 코드} catch(Exception e ){예외시 실행할 코드}
- Exception class
- .getMessage() : 오류에 대한 기본적인 내용을 출력해준다.
- .toString() : 더 자세한 예외 정보를 제공한다
- .printStackTrace() : 메소드가 내부적으로 예외 결과를 화면에 출력한다.
- try-catch 다중 예외처리 : 예외의 종류에 따라서 실행코드를 달리하고 싶을 경우 사용
- try{ 예외가 발생할 수 있는 코드} catch(예외클래스 인스턴스1; ArithmeticException ) { 예외시 실행할 코드; } catch (예외클래스 인스턴스2; ArrayIndexOutOfBoundsException){예외시 실행할 코드} cath(Exception e){예외시 실행할 코드}
- finally : try{ 예외가 발생할 수 있는 코드} catch(구체적인 예외 case ) { 예외시 실행할 코드} finally {예외와 관계없이 항상실행되는 코드}
- 사용자 정의 Exception : class를 생성하고 RuntimeException을 extends해준다. checked exception은 transaction을 rollback해주지 않음 그리고 무엇이든 throw/try-catch해주어야하는 불편함이 존재하기 때문에 unchecked exception인 runtimeexception을 extends한다
Nullpointexcpetion
- Common Sources of null Values:
- Uninitialized Variables
- Method Returning null
- Database Queries Returning null
- How to Prevent NullPointerException:
- UseOptional
- java.util.Optional
- Optional<T> : Optional이라는 구현체를 return해서 inflow가 존재하지 않더라도 null을 반환하지않고 empty optional을 return함. List를 반환하는 쿼리에는 null이 아니라 empty list가 발생하므로, single value를 return하는 메소드에 적합함.
- .ofNullable() : to safely create an Optional instance that may or may not contain a null value.
- isPresent(): Returns true if there is a value present, otherwise false.
- get(): Returns the value if present; throws NoSuchElementException if no value is present.
- filter(list -> !list.isempty) : empty가 아닌 list만 통과시키고 그 외에는 optional.empty()를 반환함.
- orElse(T other): Returns the value if present; returns other otherwise.
- orElseGet(Supplier<? extends T> other): Returns the value if present; returns the result produced by the supplier otherwise.
- orElseThrow(Supplier<? extends X> exceptionSupplier): Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier.
- 여기서 throw가 되면 exceptionhandler가 제어흐름을 받아서 예약된 로직을 수행함.
- Optional<T> : Optional이라는 구현체를 return해서 inflow가 존재하지 않더라도 null을 반환하지않고 empty optional을 return함. List를 반환하는 쿼리에는 null이 아니라 empty list가 발생하므로, single value를 return하는 메소드에 적합함.
- java.util.Optional
- NULLCHECKING
- 예외상황 checking
- ObjectUtils.isEmpty(company) vs company == null
- company == null - Basic Check: This is a straightforward nullity check. It simply tests whether the company object reference is null. This means it checks if the object exists at all.
- ObjectUtils.isEmpty(company) - Advanced Check: It checks if the object is either null, empty, or in some cases, based on the type of the object, if it has a default uninitialized state.
- ObjectUtils.isEmpty(company) vs company == null
- 예외상황 checking
- UseOptional
Best Practices: When to Log in a Spring Boot Application
- INFO : Log Application Start & Stop
- INFO : Log Incoming HTTP Requests & Responses
- INFO : Log Important Business Logic Events
- INFO : Log External API Calls
- DEBUG : Log Database Queries (When Necessary)
- INFO : Log Scheduled Jobs & Background Tasks
- ERROR : Log Exception Handling
- WARN : Log Security-Related Events
Best Practices: When to Use try catch in a Spring Boot Application
- to Customize log an error, return a specific response,
- To Recover from an Exception (Retry Logic)
- or retry an operation before throwing a new exception.
- To Clean Up Resources (Using finally)
- When Calling External APIs (Handling API Failures)
- Handling Multiple Exceptions in One Block
'개발기술 > Java' 카테고리의 다른 글
Java NIO (0) | 2025.04.06 |
---|---|
Java Blocking I/O vs non-Blocking I/O (0) | 2025.04.06 |
Java의 메모리 영역과 static의 의미 (0) | 2025.01.15 |
Java 코딩구현 심화 - 스트림,람다식 (0) | 2024.09.20 |
Java 멀티쓰레드 (0) | 2024.08.12 |