본문 바로가기
study/Java

[Java] 26. Exception8 (예외 클래스 생성)

by 금이패런츠 2022. 3. 22.
728x90
반응형
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("비밀번호를 입력하세요.");
			String inpw = scan.nextLine();
			if(id.equals(inId) && pw.equals(inpw)) {
				System.out.println("로그인 성공");
			} else if(!id.equals(inId)) {
				throw new LoginFailException("아이디가 틀립니다.");
			} else {
				throw new LoginFailException("비밀번호가 틀립니다.");
			}
		} catch (LoginFailException e) {
			System.out.println(e.getMessage());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
728x90
반응형