청춘만화 2012. 2. 24. 00:11




//새로운 예외를 정의


class UseException extends Exception{    //상속은 =Exception
public UseException(String str){  //생성자= class와 같은 이름
super(str);  //반드시 첫번째 라인으로 구성!!!
}
} // 상속관계 : object -> thowable -> Exception -> UseException




//오류아닌경우
public class ExceptionTEst {

public static void main(String[] args) {
System.out.println("Main함수안:");
int i =11; //지역변수+ 값
UseException u; 
try{
if(i==10) throw new UseException("함수안");
}catch (UseException e) {
// TODO: handle exception
System.out.println("에러");
System.out.println("----------");
System.out.println(e.toString());
System.out.println("----------");
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("Main함수안입니다.");
}
}









/*
//오류의 경유
public class ExceptionTEst {

public static void main(String[] args) {
System.out.println("Main함수안:");
int i =10;                   //지역변수+ 값
//UseException u;      // u 라는 참조변수를 별도로 저장하지 않아도 된다.
// + 다른 class의 맴버함 수의 이름과 같은 경우 대비 !
try{
if(i==10) throw new UseException("함수안");  // u 라는 참조변수를 별도로 저장하지 않아도 된다.
}catch (UseException e) {
// TODO: handle exception
System.out.println("에러");
System.out.println("----------");
System.out.println(e.toString());
System.out.println("----------");
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("Main함수안입니다.");
}
}
**/








/*
//2
public class ExceptionTEst {

public static void main(String[] args) {
System.out.println("Main함수안입니다.");
int i =10; //지역변수+ 값
UseException u = new UseException("함수안");
try{
if(i==10) throw u;
}catch (UseException e) {
// TODO: handle exception
System.out.println("에러");
System.out.println("----------");
System.out.println(e.toString());
System.out.println("----------");
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("Main함수안입니다.");
}
}
**/







/*
//1
public class ExceptionTEst {

public static void main(String[] args) {
System.out.println("Main함수안입니다.");
int i =10; //지역변수+ 값
if(i==10)
try {
throw new UseException("오류");
} catch (UseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Main함수안입니다.");

}
}**/