Error와 Exception의 상위 클래스

Throwable
OutOfMemoryErrorStackOverflowErrorVirtualMachineErrorRuntimeExceptionNullPointerExceptionArrayIndexOutOfBoundsExceptionClassCastExceptionIOExceptionSQLExceptiontry-catch 구문으로 예외를 처리해야 하는 부분try {
// 예외를 찾을 수 있는 코드 넣기
} catch (ExceptionType2 e1) {
// 오류 발생 시 실행할 구문
} catch (ExceptionType2 e2) {
// 한 try에 여러 catch 사용 가능
} finally {
// 오류 발생 여부 관계 없이 반드시 실행
}
public void myMethod() throws IOException {
}
public class MyCustomException extends Exception {
public MyCustomException(String Message){ // 생성자
super(message);
} → Exception 클래스 상속 받기public class TestCustomException {
public void myMethod() thorws MyCustomException{
throw new MyCustomException("Custom error message");
}
}public static void main(String[] args) {
TestCustomException test = new TestCustomException();
try{
test.myMethod();
} catch (MyCustomException e) {
System.out.println("Caught custom exception: " + e.getMessage());
}
}