728x90 반응형 전체 글565 [Java] 26. Exception7 (오버라이딩에서 예외 처리) package chap9; /* * 오버라이딩에서 예외 처리 */ class Parent { public void method() throws RuntimeException { System.out.println("Parent 클래스의 method 메서드"); } } class Child extends Parent { public void method() throws RuntimeException { System.out.println("Child 클래스의 method 메서드"); } } public class ExceptionEx7 { public static void main(String[] args) { Child c = new Child(); c.method(); } } 2022. 3. 22. [Java] 26. Exception6 (throw 예제) package chap9; /* * throw 예제 */ public class ExceptionEx6 { public static void main(String[] args) { try { //throw : JVM으로 예외 통지함. throw new Exception("예외 강제 발생"); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } System.out.println("프로그램 종료"); } } 2022. 3. 22. [Java] 26. Exception5 (throw) package chap9; /* * throw : 예외 발생 => 없는 예외를 강제발생 * throws : 예외 처리 => 있는 예외를 처리 * * 예외 처리 * try catch finally : try 블럭에서 예외가 발생되면, catch 구문으로 제어 이동. * finally 블럭은 무조건 실행 구문 * throws : 호출한 메서드로 예외 전달. * * 예외 발생 * throw : 예외 강제 발생 * */ public class ExceptionEx5 { public static void main(String[] args) { try { first(); } catch (Exception e) { System.out.println("main : 숫자만 가능합니다."); e.printStackTrac.. 2022. 3. 22. [Java] 26. Exception4 (throws) package chap9; /* * throws : 예외처리 * 예외 던지기 => 현재 메서드를 호출한 메서드로 예외 전송 */ public class ExceptionEx4 { public static void main(String[] args) { try { first(); } catch (Exception e) { System.out.println("숫자만 가능합니다."); e.printStackTrace(); } } private static void first() throws Exception { System.out.println("first 메서드"); second(); } private static void second() throws Exception { System.out.println(.. 2022. 3. 22. 이전 1 ··· 90 91 92 93 94 95 96 ··· 142 다음 728x90 반응형