10 / 0(0으로 나누기)throw 키워드를 통해 발생시킨다.예외처리(try-catch)를 통해 프로그램이 안정적으로 실행되게 할 수 있다.10 / 0 연산을 수행하면서 ArithmeticException(산술예외)가 발생하고 프로그램이 비정상적으로 종료된다.public class Main {
public static void main(String[] args) {
System.out.println("프로그램 시작");
int result = 10 / 0; // ❌ 예외 발생 (ArithmeticException)
System.out.println("이 문장은 실행되지 않음");
}
}
콘솔 메세지
Exception in thread "main" java.lang.ArithmeticException: / by zero
at chapter3.exception.Main.main(Main.java:8)
Process finished with exit code 1
age < 18 조건을 만족하면 IllegalArgumentException을 발생시킨다.throw를 활용하면 특정 상황에서 예외를 명확하게 정의하고 제어할 수 있다.public class Main {
public static void main(String[] args) {
int age = 10;
if (age < 18) {
// ✅ 의도적으로 예외를 발생시키는 부분
throw new IllegalArgumentException("미성년자는 접근할 수 없습니다!");
}
System.out.println("....");
}
}
RuntimeException 을 상속받는 모든 예외를 UncheckedException 이라고 한다.Exception 클래스를 직접 상속받는 모든 예외를 CheckedException 이라고한다. RuntimeException과 RuntimeException 을 상속받은 예외는 제외한다.
main()) 까지 전파되고 끝내 처리되지 않으면 프로그램이 비정상 종료 된다.RuntimeException 을 상속받는 모든 예외를 UncheckedException 이라고 한다.✅ try-catch 활용
public class ExceptionPractice {
public void callUncheckedException() {
if (true) {
System.out.println("언체크 예외 발생");
throw new RuntimeException(); // ✅ 예외발생
}
}
}
public class Main {
public static void main(String[] args) {
// 예외 실습 객체 인스턴스화
ExceptionPractice exceptionPractice = new ExceptionPractice();
// ✅ 언체크 예외 호출
exceptionPractice.callUncheckedException();
// ❌ 예외처리를 해주지 않았기 때문에 프로그램이 종료됩니다.
System.out.println("이 줄은 실행되지 않습니다.");
}
}
public class Main {
public static void main(String[] args) {
ExceptionPractice exceptionPractice = new ExceptionPractice();
// ✅ 상위로 전파된 예외처리
try {
exceptionPractice.callUncheckedException();
} catch (RuntimeException e) { // ✅ 예외처리
System.out.println("언체크 예외 처리");
} catch (Exception e) {
System.out.println("체크 예외 처리");
}
System.out.println("프로그램 종료");
}
}
Exception 클래스를 직접 상속받는 모든 예외를 CheckedException 이라고 한다.RuntimeException 을 상속받는 예외는 제외try-catch로 예외를 처리하거나 throws 키워드를 사용해야 한다.throws 로 예외 처리의 책임을 호출자에게 전가할 수 있다.✅ try-catch 활용
CheckedException을 try-catch를 사용하여 직접 처리하는 방식이다.public class ExceptionPractice {
public void callCheckedException() {
// ✅ try-catch 로 예외 처리
try {
if (true) {
System.out.println("체크예외 발생");
throw new Exception();
}
} catch (Exception e) {
System.out.println("예외 처리");
}
}
}
public class Main {
public static void main(String[] args) {
// 예외 실습 객체 인스턴스화
ExceptionPractice exceptionPractice = new ExceptionPractice();
// ✅ 체크예외 호출
exceptionPractice.callCheckedException();
}
}
✅ throws 활용
public class ExceptionPractice {
public void callCheckedException() throws Exception { // ✅ throws 예외를 상위로 전파
if (true) {
System.out.println("체크예외 발생");
throw new Exception();
}
}
}
package chapter3.exception;
public class Main {
public static void main(String[] args) {
// 예외 실습 객체 인스턴스화
ExceptionPractice exceptionPractice = new ExceptionPractice();
// 체크 예외 사용
// ✅ 반드시 상위 메서드에서 try-catch 를 활용해 주어야합니다.
try {
exceptionPractice.callCheckedException();
} catch (Exception e) {
System.out.println("예외처리");
}
}
}