- 오버라이딩에서 예외처리
📌
package chap9;
/*
* 오버라이딩에서의 예외처리:
* 부모클래스의 예외처리와 같거나 작은범위(하위예외객체) 가능.
*/
class Parent {
public void method() throws RuntimeException {
System.out.println("Parent 클래스의 method()");
}
}
class Child extends Parent {
public void method() throws ArithmeticException {
System.out.println("Child 클래스의 method()");
}
}
public class ExceptionEx6 {
public static void main(String[] args) {
Child c = new Child();
c.method();
}
}
- 예외클래스 생성
📌
package chap9;
import java.util.Scanner;
public class ExceptionEx7 {
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.next();
System.out.println("비밀번호를 입력하세요");
String inPw = scan.next();
if(id.equals(inId) && pw.equals(inPw)) {
System.out.println("로그인 성공");
} else if(!id.equals(inId)) {
throw new LoginFailException("ID 확인하세요");
} else {
throw new LoginFailException("비밀번호를 확인하세요");
}
} catch (LoginFailException e) {
System.out.println(e.getMessage());
} catch(Exception e) {
e.printStackTrace();
}
}
}
class LoginFailException extends Exception{
LoginFailException(String msg) {
super(msg);
}
}
'수업(국비지원) > Java' 카테고리의 다른 글
| [Java] chap10 : 내부 클래스 - 내부 클래스 (0) | 2023.04.16 |
|---|---|
| [Java] chap9: 예외처리 - Exception Exam 1-3 (0) | 2023.04.16 |
| [Java] chap9 : 예외처리 - Exception5(throw) (0) | 2023.04.15 |
| [Java] chap9 : 예외처리 - Exception4(throws) (0) | 2023.04.15 |
| [Java] chap9 : 예외처리 - Exception3(finally) (0) | 2023.04.15 |