读书人

错误(二)

发布时间: 2012-12-20 09:53:21 作者: rapoo

异常(二)

?

1.异常的捕获和处理:

?

public class Try2{public static void main(String args[]){int i=0;int a[]={5,6,7,8};for(i=0;i<5;i++){try{System.out.print("a["+i+"]/"+i+"="+(a[i]/i));}catch (ArrayIndexOutOfBoundsException e)//数组下表越界{System.out.print("捕获数组下标越界异常");}catch(ArithmeticException e){System.out.print("捕获算数异常");}catch(Exception e){System.out.print("捕获"+e.getMessage()+"异常");}finally{System.out.println(" i="+i);}}System.out.println("继续!");}}
?

?

结果:

捕获算数异常 i=0

a[1]/1=6 i=1

a[2]/2=3 i=2

a[3]/3=2 i=3

捕获数组下标越界异常 i=4

继续!

?

?

?

2.求数组元素的平均值:

?

public class ArrayAverage2{public static double average(int table[]){ //求数组元素平均值if(table!=null&&table.length!=0){double sum=0;for(int i=0;i<table.length;i++){sum+=table[i];}return sum/table.length;}return 0.0;}public static double average(int table1[],int table2[]){//计算两个数组的平均值int count=0;if(table1!=null&&table1.length!=0)count=table1.length;if(table2!=null&&table2.length!=0)count+=table2.length;if(count!=0)return(average(table1)*table1.length+average(table2)*table2.length)/count;elsereturn 0.0;}public static int[] tointArray(String str[]){//获得字符串中的整数值        if(str!=null&&str.length!=0){int temp[]=new int[str.length];int count=0;for(int i=0;i<str.length;i++){try{temp[count]=Integer.parseInt(str[i]);  count++;}catch (NumberFormatException e){System.out.println("字符串"+str[i]+"不能转化为整数,产生异常类"+e.getClass().getName());}}int table[]=new int[count];System.arraycopy(temp,0,table,0,count);  //复制数组return table;}return null;}public static void print(int table[]){//输出数组元素          if(table!=null){  System.out.print("数组元素为:");  for(int i=0;i<table.length;i++)  System.out.print(" "+table[i]);  System.out.println();  }}public static void main(String args[]){int x[]={1,2,3,4};int y[]={};System.out.println("average(x,y)="+average(x,y));y=tointArray(args);print(y);System.out.println("average(y)="+average(y));}}
?

?

结果:

?

average(x,y)=2.

average(y)=0.0

?

3.抛出异常;

public class Person2{protected String name;protected int age;public Person2(String name,int age) throws Exception  //构造方法声明抛出异常{//无法处理set()方法抛出的异常,再向上传递set()方法抛出的异常         this.set(name); this.set(age);}public void set(String name){if(name==null||name=="")this.name="姓名未知";elsethis.name=name;}public void set(int age) throws Exception{if(age>=0&&age<100)this.age=age;elsethrow new Exception("IllegalAgeData"+age);}public void set(String name,int age) throws Exception{this.set(name);this.set(age);}public String toString(){return this.name+","+this.age+"岁";}public void print(){    System.out.println(this.toString());}public static void main(String args[]) throws Exception{Person2 p1=new Person2("李小明",20);p1.print();p1.set(121);p1.print();}}
?

?

结果:

李小明,20岁

Exception in thread "main" java.lang.Exception: IllegalAgeData121

? ? ? ? at Person2.set(Person2.java:23)

? ? ? ? at Person2.main(Person2.java:41)

?

?

4.自定义异常类:

?

//声明自定义异常类只需要声明构造方法public class IllegalAgeException extends Exception{    public IllegalAgeException(String s){super(s);}public IllegalAgeException(){this("");}}
?

public class Person3{protected String name;protected int age;public Person3(String name,int age) throws IllegalAgeException{this.set(name);this.set(age);}public void set(String name){if(name==null||name=="")this.name="姓名未知";elsethis.name=name;}public void set(int age) throws IllegalAgeException{if(age>=0&&age<=100)this.age=age;elsethrow new IllegalAgeException(""+age);}public void print(){    System.out.println(this.name+","+this.age+"岁");}public static void main(String args[]){Person3 p1=null;try{p1=new Person3("李小明",20);p1.set(121);}catch (IllegalAgeException e) //捕获自定义异常类{e.printStackTrace();//显示异常栈跟踪信息}finally  //一定要执行的语句{p1.print(); }}}
?


错误(二)

?

?

读书人网 >编程

热点推荐