예외

Moon·2024년 2월 25일

Java

목록 보기
17/45

throw

Checked Exception

  • Exception 클래스를 직접 상속받음. (RuntimeException 의 하위 클래스가 아닌 Exception 클래스의 하위 클래스)
  • 컴파일 시점에 확인된 exception
  • 반드시 try - catch 구문 사용해야 컴파일이 됌.'
  • 사용자의 실수와 같은 외적인 요인에 의해 발생.
  • 예외처리 필수.
  • IOException: InputStream, OutputStream 관련 메소드 사용할 때.
    주로 throws IOException으로 사용.
  • InterruptedException: 주로 throws InterruptedException으로 사용.
  • FileNotFoundException,
  • ClassNotFoundException

Unchecked Exception

  • extends **RuntimeException**
  • 강제적으로 try - catch 구문 사용하지 않아도 됌.
  • 프로그램 실행 도중(런타임)에 발생할 수 있는 예외들임.
  • 프로그래머의 실수로 발생.
  • 예외처리 선택.
  • NullPointerException
  • IndexOutOfBoundsException
  • ClassCastException
class Warrior {
}

class SuperWarrior extends Warrior {
}

Warrior warrior = new Warrior(); //에러나는 코드
// Warrior warrior = new SuperWarrior(); //맞는 코드
SuperWarrior superWarrior = (SuperWarrior) warrior;
// 생성된 인스턴스는 Warrior 이므로 SuperWarrior로 형 변환시
// ClassCastException 발생
  • InputMismatchException
  • ArithmeticException
  • IllegalArgumentException

try - catch - (finally)

Exception catch 블럭은 제일 마지막에!

.printStackTrace(), .getMessage()

throws

업로드중..

0개의 댓글