Java
[자바 기초] throws 예제소스 java
TheOtherSideDoor
2010. 6. 25. 09:38
예제 소스
//Throw 구문을 이용한 예외 처리 예제 프로그램
public class ExceptionExam2 {
public static int divide(int num1,int num2)throws ArithmeticException{
int result =0;
result = num1/num2;
return result;
}
public static void main(String[] args) {
int result =0;
try{
result = divide(10,5); //1번 째 함수 실행 오류
System.out.println("1번째 실행입니다. result : "+result);
result = divide(10,0); //2번 째 함수 실행 오류 예외 영역으로 넘어감..
System.out.println("2번째 실행입니다. result : "+result);
}catch(ArithmeticException e){
System.out.println("오류 입니다."+e);
}
}
}
실행 결과
1번째 실행입니다. result : 2
오류 입니다.java.lang.ArithmeticException: / by zero
실행환경 jdk 6.0 실행 tool 이클립스 갈릴레오
*실행 원리를 이해하세요..