2012년 12월 30일 일요일

[Ch13Ex08] throws 절로 예외 처리 미루기

Result

>>main 함수에서 예외를 발생시키는 함수  호출 실험하기<<
강제로 exception 으로 던졌어요. throw Exception 함수 호출 했거든요. catch로 가겠죠?
>> catch 구문 : 예외를 처리한다. <<
java.lang.Exception:  private error 나만의 에러래요
>>정상종료<<




Sample code


package kame.ch13.ex01;
public class Ch13Ex08 {

public static void throwException( ) throws Exception {
System.out.println("강제로 exception 으로 던졌어요. " +
"throw Exception 함수 호출 했거든요. catch로 가겠죠?");
throw new Exception(" private error 나만의 에러래요");
}

public static void main(String[] args) {
try{
System.out.println(">>main 함수에서 예외를 발생시키는 함수  호출 실험하기<<");
throwException( );
}catch(Exception e){
System.out.println(">> catch 구문 : 예외를 처리한다. <<");
System.out.println(e);    
}
System.out.println(">>정상종료<<");
}
}

/*
throw : 예외를 일부러 발생 시킬때 throw new 예외_클래스()
throws : 발생된 예외를 해당 메소드에서 직접 처리하지 않고 이를 호출한 객체로
처리를 미루기 위해서 사용한다.
public voide 메서드이름(매개변수) throws Exception{}
*/

Reference book 이클립스로 배우는 Java 6 Programming/ 임종권, 전병찬/ 가메출판사

댓글 없음:

댓글 쓰기