예외처리는 프로그램에서 에러가 발생했을 때, 프로그램이 중단되는 것을 막기위한 처리 기법이다.
아무리 오류 없이 잘 돌아가게 만들었다해도 오류가 발생하지 않을것이라 장담하긴 어려워 우리는 예외처리를 해줘서 프로그램 셧다운을 막아야한다.


public class Exception extends Throwable {
public static void main(String[] args) {
try {
// 예외가 발생할 수 있는 코드
System.out.println("Exception 01 ========");
throw new java.lang.Exception();
} catch (java.lang.Exception e) {
// 예외 처리 코드
System.out.println("Exception 02 ========");
} finally {
// 예외와 상관없이 무조건 실행되는 코드
System.out.println("Exception 03 ========");
}
}
}
public class Exception {
public static void main(String[] args) {
int num1 = 10;
int num2 = 4;
try {
throwTest(num1, num2);
} catch (ArithmeticException e) {
System.out.println("AritheticException : " + e.getMessage());
}
}
public static void throwTest(int a, int b) throws ArithmeticException {
throw new ArithmeticException();
}
}
위의 예제의 생성자를 보면 값이 null일 때 throw를 통해서 강제로 에러를 발생시킨다.
만약, 강제로 에러를 발생시켰다면 try ~ catch로 예외처리를 하게 된다.
try ~ catch로 해결하기 싫다면 throws를 사용하여 호출한 곳으로 에러를 던지면 된다.
public class Exception {
public static void main(String[] args) {
int num1 = 10;
int num2 = 4;
try {
throwTest(num1, num2);
} catch (ArithmeticException e) {
System.out.println("AritheticException : " + e.getMessage());
}
}
public static void throwTest(int a, int b) throws ArithmeticException {
System.out.println("throw a/b : " + a/b);
}
}
위 방법은 ArithmeticException 예외가 발생한다면 <메세지를 호출한 곳>에 예외처리를 던져버리는 것이다.
그렇다면 <메세지를 호출한 곳>에서 반드시 try ~ catch로 메서드 호출부분을 감싸줘야한다.
그렇지 않으면 예외처리를 양쪽에서 아무도 하지 않는 것이다.
그러므로 이 헷갈림과 결국엔 try ~ catch를 사용하는 이유로 잘 사용하지 않는다.

자바에서 발생할 수 있는 프로그램 오류를 "에러"와 "예외"로 구분했다
예외중 RuntimeException 클래스들은 주로 프로그래머의 실수에 의해서 발생될 수 있는 예외들로서 예를 들면 배열의 범위( IndexOutOfBoundsException ), 값이 null인 경우( NullPointerException ) 등의 경우에 발생하는 예외들이다.
예외중 Exception 클래스들은 주로 외부의 영향으로 발생할 수 있는 것들로 예를 들면 FileNotFoundException, ClassNotFoundException, DataFormatException 등이 있다. 이런 종류의 외부에서 영향을 받을 수 있는 것들은 반드시 예외 처리를 해줘야한다.
public class CustomException extends RuntimeException{
CustomException() {
}
// 예외 발생 원인을 전달하기 위해 String 타입의 매개변수를 가진 생성자
CustomException(String message) {
super(message); // RuntimeException 클래스의 생성자를 호출.
}
}
public class CustomException extends RuntimeException{
CustomException() {
}
// 예외 발생 원인을 전달하기 위해 String 타입의 매개변수를 가진 생성자
CustomException(String message) {
super(message); // RuntimeException 클래스의 생성자를 호출.
}
public static void main(String[] args) {
try {
test();
} catch (CustomException e) {
System.out.println("커스텀 예외 테스트");
}
}
public static void test() throws CustomException {
throw new CustomException("예외 테스트");
}
}
한 페이지에 작성하려다보니 조금 난잡해졌다..
위 예제처럼 custom으로 사용할 수 있다는 것을 알아두자
getMessage() :
printStackTrace() :