IT/Java

예외처리

알라이또 2017. 1. 21. 12:02
반응형

 







class 합이백을넘는오류 extends Exception { //아무의미 없고 단순히 이런 인셉션이 일어낫다
}
class 값이오십을넘는오류 extends Exception {
}


class Calc {
public int add(int x,int y) throws 합이백을넘는오류, 값이오십을넘는오류{
int result = x+y;
if(result > 100)
throw new 합이백을넘는오류(); //던진다 누구한테? throws 문뒤에있는애한테 (없으면 자기자신)
if(x > 50 || y>50)
throw new 값이오십을넘는오류(); //던진다 누구한테? throws 문뒤에있는애한테 (없으면 자기자신)
return result;
}
public int sub(int x,int y) {
int result = x-y;
return result;
}
public int multi(int x,int y) {
int result = x*y;
return result;
}
public int div(int x,int y) {
int result = x/y;
return result;
}
}
public class Program {
public static void main(String[] args) {// throws 합이백을넘는오류 { //자바런타임한테 던짐
Calc calc = new Calc();
int result = 0;
try {
result = calc.add(51, 49);
} catch (합이백을넘는오류 e) {
System.out.println("합이 100을 넘을수 없다 ");
} catch (값이오십을넘는오류 a) {
System.out.println("값이 50을 넘을수 없다 ");
}
finally {
System.out.println("자원중지");
}
System.out.println(result);
}
}























--------------------------------------