기타/What I Learned

[TIL] 자료구조 - 예외(Exception)

가죽방패 2022. 3. 14. 09:49

※ 예외(Exception)

// Exception 클래스 상속
public class FileFormatException extends Exception{
	public File FormatException () {
    // super 호출
    super();
  	}
    public FileFormatException (String s){
    	super(s);
    }
}

// 예외 상황이 발생하면 throw
throw new FileFormatException("Your file is not well formatted")

위 코드와 같이 Exception 클래스를 상속받고 생성자를 만든 후, 생성자 안에서 super를 호출하면 예외 상황에 대한 클래스를 만들 수 있다. 이후 예외 상황이 발생했을 때 throw 사용하면, 예외 상황의 이름으로 에러가 발생한다.