12. What is the difference between throw and throws in Java Exception Handling ?

toutbon·2024년 3월 4일
0

질문의 의도

throw 와 throws 를 구분할 수 있는가

throws

Exception handling method

  • try/catch : 블럭으로 바로 해결
  • throws : caller 메서드에게 책임 전가 함. 메서드의 선언부에 throws 사용함
  • 사용법 : throws ExceptionA, ExceptionB
  • class declaration -> 선언부에서 사용해야함
  • method signature
  • can declare multiple exceptions

throw

  • 예외 상황을 발생시키고자 할 때 사용
  • 개발자가 인위적으로 발생시킨다.
    - 예 > 서버의 응답이 너무 오래 걸리는 경우 timeout exception을 인위로 던져줌
    throw new TimeOutException(No Response From Server)
  • exception 에 따라 개별적으로 handling이 필요함
  • 사용법 : throw new ExceptionA()
  • an object
  • within method
  • only single exception

KeyWord

throws

  • declare exception class name at method signiture
  • single or multiple exceptions thorws

throw

  • create an exception object and throw it explicitly
  • within method
  • single exception throw
profile
뚜봉

0개의 댓글