본문 바로가기
728x90
반응형

exception8

[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. 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.
[Java] 26. Exception3 (finally) package chap9; /* * finally 블럭 : try구문에서 정상/예외 모두 실행 되는 블럭 * 메서드 중간에 return을 만나도 실행됨. * 예외발생 : 1456 * 정상 : 12356 * 예외발생 catch 구문에 return : 145 * 정상 try 블럭에 return : 1235 */ public class ExceptionEx3 { public static void main(String[] args) { try { System.out.print(1); System.out.print(2); //정상 //System.out.print(2/0); //예외발생 System.out.print(3); return; } catch (Exception e) { System.out.print(4.. 2022. 3. 22.
[Java] 26. Exception1 (try, catch) package chap9; /* * 예외처리 : 발생된 예외를 정상화 하는 방법 * try catch 구문 * try 블럭 : 예외 발생 가능성이 있는 구문이 있는 블럭 * catch 블럭 : try블럭에서 예외 발생시 실행되는 블럭 */ public class ExceptionEx1 { public static void main(String[] args) { //System.out.println(args[1]); //ArrayIndexOutOfBoundsException 예외발생 try { System.out.println(10/0); //ArithmeticException 예외발생 System.out.println(args[0]); //ArrayIndexOutOfBoundsException 예외발생 .. 2022. 3. 22.
728x90
반응형