728x90 반응형 java271 [Java] 26. Exception9 (예외처리를 생략해도 되는 예외 클래스 생성하기) package chap9; import java.util.Scanner; //예외처리를 생략해도 되는 예외 클래스 생성하기 class LoginFailException extends RuntimeException { LoginFailException(String msg) { super(msg); } } public class ExceptionEx9 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("로그인 되었습니까?"); String login = scan.nextLine(); if(login.equals("yes")) { System.out.println("반갑습니다.").. 2022. 3. 22. [Java] 26. Exception8 (예외 클래스 생성) package chap9; import java.util.Scanner; /* * 예외 클래스 생성 */ class LoginFailException extends Exception { LoginFailException(String msg) { super(msg); } } public class ExceptionEx8 { public static void main(String[] args) { try { String id = "hong"; String pw = "1234"; Scanner scan = new Scanner(System.in); System.out.println("id를 입력하세요."); String inId = scan.nextLine(); System.out.println("비밀번호를 .. 2022. 3. 22. [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. 이전 1 ··· 23 24 25 26 27 28 29 ··· 68 다음 728x90 반응형