본문 바로가기
728x90
반응형

java271

[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.
[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. Exception2 (다중 catch 구문) package chap9; /* * 다중 catch 구문 * - 한개의 try블럭에 여러개의 catch블럭이 존재. * - try블럭내에서 발생 가능한 예외가 여러 종류인 경우, 예외별로 예외처리 가능 * - 상위 예외클래스는 catch 구문의 하단에 배치해야 한다. */ public class ExceptionEx2 { public static void main(String[] args) { try { String str = null; //객체없음. //System.out.println(str.charAt(0)); //NullPointerException 예외발생 //int 2022. 3. 22.
728x90
반응형