[java] 예외 발생시키기: throw 키워드의 역할은 무엇인가요?
예를 들어, 다음과 같이 코드를 작성할 수 있습니다.
public void divide(int numerator, int denominator) {
if (denominator == 0) {
throw new ArithmeticException("분모는 0일 수 없습니다.");
}
// 나누기 연산을 수행
}
위 코드에서, denominator
가 0일 경우 ArithmeticException
을 발생시키고 있습니다.
throw
키워드를 이용해 예외를 발생시킴으로써 메소드는 예외를 처리하는 부분으로 제어를 전달하게 됩니다.
더 자세한 내용은 다음 문서를 참고하실 수 있습니다.
Oracle Java Documentation: The throw
Statement