2007年2月10日星期六

[破碎细胞]自定义异常(Exception)练习代码

代码:
-------------------------------------------------


//Name:MyException1
//Description:MyException1
//Auther:CrackCell
//Date:10-Feb-07

class MyException_1 extends Exception{
MyException_1(String ErrorMessage){
super(ErrorMessage);
}
}

class MyException_2 extends Exception{
MyException_2(String ErrorMessage){
super(ErrorMessage);
}
}


public class MyException{
static int p(int a) throws MyException_1,MyException_2{
switch(a){
case 1:
throw new MyException_1("a equals 1.");
case 2:
throw new MyException_2("a equals 2.");
default:
return(a*10);
}
}

public static void main(String[] args){
try{
System.out.println(p(1));
}catch(MyException_1 me1){
System.out.println("MyException_1 processed.");
}catch(MyException_2 me2){
System.out.println("MyException_2 processes.");
}try{
System.out.println(p(2));
}catch(MyException_1 me1){
System.out.println("MyException_1 processed.");
}catch(MyException_2 me2){
System.out.println("MyException_2 processed.");
}finally{
System.out.println("Exception caught");
}
}
}



-------------------------------------------------
结果:
MyException_1 processed.
MyException_2 processed.
Exception caught

没有评论: