Exception01
import java.util.Scanner;
public class Exception01 {
Scanner sc =new Scanner(System.in);
public static void main(String[] args) {
Exception01 e = new Exception01(); //Exception01객체생성
e.process();
e.process2();
e.process3();
e.process4();
}
public void process() {
int a = 10;
int b = 0;
//런타임 Exception
//ArithmeticException:
if(b==0) { //b가
System.out.println("0으로는 나눌수 없습니다.");
return;// 메소드 즉시 종료
}
System.out.println(a/b);
}
Exception02
public class Exception02 {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
Exception02 e2 = new Exception02();
e2.method1();
}
public void method1() {
while(true) {
try {
String id = inputId(); // 에러 처리 여기서 함
String Pass = inputPass(); // 에러 처리 여기서 함
System.out.println("id : "+id);
System.out.println("Pass : "+Pass);
break;// try문 true면 종료
} catch (Exception e) {
System.out.println(e);
}
}
}
public String inputId() throws NicNameException { //methdo1에서 에러 처리하게 던짐
System.out.println("입력하시오");
String id = sc.next();
if(id.contains("aa")) {
throw new NicNameException();
}
if(id.length()>10) {
throw new NicNameException();
}
return id;
}
public String inputPass() throws NicNameException { //methdo1에서 에러 처리하게 던짐
System.out.println("입력하시오");
String id = sc.next();
if(id.contains("aa")) {
throw new NicNameException();
}
if(id.length()>10) {
throw new NicNameException();
}
return id;
}
}
NicNameException
public class NicNameException extends RuntimeException{
public NicNameException() {
super("부적절한 닉네임 입니다.");
}
}