JAVA/Java

IllegalStateException VS IllegalArgumentException

dodop 2022. 2. 13. 18:39

 

 

 

 

예외 처리를 부분을 작성하면서 IllegalStateException과 IllelgalArgumentException의 차이는 무엇인지,

어떤 상황에서 사용해야하는 지 궁금해졌다. 

 

 

 

 

IllegalStateException
Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

자바 정규 문서를 보면 적절하지 않거나 유효하지 않은 시간대에 메소드가 불러졌을 때 발생하는 예외로 정의되어있다. 즉, 죽은 쓰레드로 부터 불려지는 경우와 같이 메소드가 불려져서는 안될 시간에 메소드가 호출될 때 발생되는 예외를 말한다. 불가변한 수가 변경되려고 하거나 객체가 잘못된 상태에 있을 때 메소드를 실행시키려고 했을 때 발생된다.  - > coding Error 

 

 

 

 

 

IllegalArgumentException
Thrown to indicate that a method has been passed an illegal or inappropriate argument.

이 예외는 적절하지 않거나 유효하지 않은 데이터를 받았을 때 발생되는 예외로, 예를 들어 양수를 파라미터로 기대하고 있었는데 음수의 수가 넘어왔을 때와 같은 경우에 발생하게 된다. API를 통해 넘어온 값이 올바르지 않을 때 발생한다.   - > input Error 

 

 

 

( 참고한 사이트 )

https://stackoverflow.com/questions/48736329/when-to-throw-illegalstateexception-vs-illegalargumentexception

 

When to throw IllegalStateException vs IllegalArgumentException?

Let's start with the Javadocs: IllegalStateException Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an

stackoverflow.com