파닥이

예외처리 본문

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);
}
}























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

 


'IT > Java' 카테고리의 다른 글

[java] 예외처리  (0) 2018.12.18
ProcessBuilder 통하여 외부 프로그램 실행  (0) 2018.10.05
자바에서 xml파싱  (0) 2017.01.21
추상화해서 재정의하는(고쳐쓰는) 두가지 방법  (0) 2017.01.21
자바 JDBC  (0) 2017.01.21
Comments