异常(二)

 

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;
		else
			return 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="姓名未知";
		else
			this.name=name;
	}
	public void set(int age) throws Exception
	{
		if(age>=0&&age<100)
			this.age=age;
		else
			throw 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="姓名未知";
		else
			this.name=name;
	}
	public void set(int age) throws IllegalAgeException
	{
		if(age>=0&&age<=100)
			this.age=age;
		else
			throw 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(); 
		}
	}
}
 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值