Exception .! 간단한 예제..!! JAVA 공부!!

 
MulBumException.java
 
public class MulBumException extends Exception {
public MulBumException() {

}
public MulBumException(String msg) {
super(msg);
}
// 오버라이딩.
public String toString() {
return "물범이 진짜 무섭나요?";
}
}





TestEx2.java

Public class TestEx2 {

private void test() throws MulBumException, Exception {
// test() 를 선언한 곳에서 Exception 처리를 해야된다.!!
// 한군데에 몰아서 Exception을 처리하고자 할때. throws.!!
try {
String s = null;
String str = s.toString();
// String s 의 값이 null 이기 때문에 NullPOinterException 이 발생.
// throw new NullPointerException(); 던져지고
// Exception이 발생하면 밑에는 실행 안된다. Exception을 처리했다.!
System.out.println("실행 되는거???");
} catch(Exception e) {
// Exception e = new NulPointerException(); 받고
System.out.println("오류 발생!! 왜??");
} finally {
// 반드시 try와 같이 쓰여야 한다.
System.out.println("무조건 실행하는거다. 뽜이눨리~");
}
System.out.println("여기는 오는거??");

throw new MulBumException();
// 여기도 던지고 메소드 선언에서 던지는건 execute()에 던져진다.
}

private void execute() {
try {
test();
} catch(MulBumException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
// Exception의 종류에 따라 처리할 일이 다를때는 따로 catch!!
}

public static void main(String[] s) {
new TestEx2().execute();
}
}




트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://marcus215.egloos.com/tb/123080 [도움말]

덧글

댓글 입력 영역