Beny's Study
try ~catch 예문 본문
부모클래스
package com.kh.ex02.user_exception;
public class UseException {
public static void main(String[] args) {
String userID = "test";
String userPW = "1234";
String inputID = "test";
String inputPW = "1234";
try {
if(userID.equals(inputID) == false) {
throw new LoginException("아이디가 맞지 않습니다!");
}
if(userPW.equals(inputPW) == false) {
throw new LoginException("비밀번호가 맞지 않습니다!");
}
System.out.println("로그인 성공");
} catch (LoginException e) {
System.out.println("로그인 예외처리!");
e.printStackTrace();
} finally {//무조건 출력
// 주로 자원을 정리해주는 곳
inputID = null;
inputPW = null;
System.out.println("자원이 정리되었습니다.");//79@
}
}
}
자식클래스
package com.kh.ex02.user_exception;
public class LoginException extends Exception {
private static final long serialVersionUID = 1L;
private String cause;
public LoginException(String cause) {
this.cause = cause;
}
@Override
public void printStackTrace() {
System.out.println("로그인에 실패하였습니다.");
System.out.println("원인 : " + cause);
super.printStackTrace();
}
}
"본 인터넷 사이트 내의 모든 이미지, 문구, 콘텐츠, 내용 등에 대한 저작권은 76beny에게 있습니다.
이를 무단으로 도용, 복사, 전재, 재배포, 2차 변형 등을 할 경우
민, 형사상 법적 조치 등 저작권법에 의거하여 처벌 받을 수 있습니다."
'[JAVA] > 11.예외처리' 카테고리의 다른 글
file 권한설정메소드 _ .setWritable( ) (0) | 2022.04.20 |
---|---|
file 권한확인 메소드 _ canRead,canWrite,canExecute (0) | 2022.04.20 |
throw와 throws의 차이 (0) | 2022.04.20 |
try~catch 문 _ 예외처리 문법 (0) | 2022.04.20 |
java.lang.OutOfMemoryError: Java heap space(시스템에러_힙) (0) | 2022.04.20 |