본문 바로가기

Java

(29)
Chained Exception Chained Exception (연결된 예외)는 한 예외가 다른 예외를 발생시키는 것이다. ExceptionA가 ExceptionB를 발생시키면 ExceptionA는 ExceptionB의 원인예외 라고 한다. Throwable initCasue (Throwable cause) : 파라미터를 원인 예외로 등록 Throwable getCause () : 원인 예외를 반환 Chained Exception을 사용하는 이유 중 첫 번째는 여러 예외들을 하나의 예외로 묶어서 다루기 위해서이다. 아래 코드는 ArrayIndexOutOFBoundsException이랑 ArithmeticException을 MyUncheckedException이라는 사용자 정의 Exception을 묶어서 처리를 하고 있다. 이렇게 묶어..
Exception re-throwing, try-catch문에서의 return 예외를 양쪽에서 처리해야 할 경우 일부분만 처리하고 다시 예외를 throw 해서 사용하는 곳으로 예외를 떠기는 방식을 예외 되던지기 Exception re-throwing이라고 한다. public class Test { public static void main(String[] args) { try { method(); } catch (Exception e) { System.out.println("main(String[]) 예외가 처리됨"); } } static void method() throws Exception { try { throw new Exception(); } catch (Exception e) { System.out.println("method() 예외가 처리됨"); throw e; //..
사용자 정의 예외 사용자 정의 예외를 만들려면 Exception클래스나 RuntimeException 클래스를 상속받아야 한다. 또한, 사용자 정의 예외는 JVM에서 예외를 발생시켜 주지 않으므로 직접 예외 객체를 생성해 주어야 한다. RuntimeException을 상속받으면 마찬가지로 try-catch문이 필수가 아니지만 Exception을 상속받은 사용자 정의 클래스 이면 try-catch문이 강제된다. 예시 public class MyuncheckedExceptoin extends RuntimeException { private String errMsg; public MyuncheckedExceptoin(String errMsg) { super(errMsg); this.errMsg = "사용자 정의 에러메시지: "..
throw, checked - unchecked 예외, throw throw : 예외를 발생시키는 키워드이다. ex) throw new Exception("message"); // 예외발생 public class Test { public static void main(String[] args) { try { throw new Exception("message"); } catch (Exception e) { System.err.println("예외 메시지 출럭 : " + e.getMessage()); } } } 더보기 public class Test { public static void main(String[] args) { // 은폐의 목적으로 try-catch문 사용 try { throw new RuntimeException(); } catch (Exception e..