实验八 异常处理

实验目的

1.了解Java中常见的异常。
2.掌握自定义异常的方法。
3.了解如何在方法中抛出异常。

实验学时

2学时
实验内容

  1. 编写一个程序,要求从键盘输入一个圆的半径(double类型),计算并输出圆的面积。在没有加入异常处理机制时,输入的数据不是double型数据(如字符串“abc”)会产生什么结果?加入异常处理机制后,让程序在输入不正确的类型数据时给出错误提示并要求重新输入。

答:当输入的数据不是double型数据时会出现数据类型不匹配异常
实验代码:

package test8;
import java.util.Scanner;
import java.util.InputMismatchException;
public class exp1 {

	public static void main(String[] args){
		// TODO Auto-generated method stub
			while(true){
				try{
					Scanner s=new Scanner(System.in);
					double r=s.nextDouble();
					System.out.println("圆的面积:"+3.14*r*r);
				}
				catch(InputMismatchException e){
					System.out.println("输入的数据不符合要求,请重新输入:");
					e.printStackTrace();
				}
			}
		
	}

}

运行结果:
在这里插入图片描述

  1. 分析下面的程序。
    (1)程序在运行时会产生哪些异常?怎样捕获并处理异常?
    答:会产生数据类型不匹配异常、除0异常,也可能会出现数组下标越界异常。
    可以用try…catch…finally语句捕获处理异常
    (2)修改代码:不管程序在执行过程中会不会产生异常,最后都输出“程序运行结束”。
    import java.util.Scanner;
    public class ExceptionSample {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int a[]=new int[5];
    int n,sum=0;
    int average;
    n=sc.nextInt();
    for(int i=0;i<n;i++){
    a[i]=sc.nextInt();
    sum=sum+a[i];
    }
    average=sum/n;
    System.out.println(average);
    }
    }

修改代码:

package test8;
import java.util.Scanner;
import java.util.InputMismatchException;
public class ExceptionSample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
	    int a[]=new int[5];
	    int n,sum=0;
	    float average;
	    System.out.println("请输入数据的个数:");
        try{
        	n=sc.nextInt();
        for(int i=0;i<n;i++){
           a[i]=sc.nextInt();
	       sum=sum+a[i];
        }
        average=sum/n;
        System.out.println("平均数:"+average);
        }
        catch(InputMismatchException e){
        	System.out.println("输入数据类型不匹配!");
        }
        catch(ArithmeticException e){
        	System.out.println("除0异常!");
        }
        catch(ArrayIndexOutOfBoundsException e) { 
			System.out.println("数组越界异常!");
		}
        finally{
        	System.out.println("程序运行结束");
        }
	}

}

运行结果:
(1)
在这里插入图片描述

(2)
在这里插入图片描述

(3) 在这里插入图片描述

  1. 设计一个程序,输入两个整数和一个算术运算符(+、-、*、/),根据运算符,计算两个整数的运算结果。考虑到用户输入的数据不合法,需要捕获异常(数据格式异常、除零异常)。
    实验代码:
package test8;
import java.util.Scanner;               
import java.util.InputMismatchException;
public class exp3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner p=new Scanner(System.in);
		try{
			int a=p.nextInt();
			int b=p.nextInt();
			String c=p.next();
			switch(c){
			case "+":
				System.out.println(a+c+b+"="+(a+b));break;
			case "-":
				System.out.println(a+c+b+"="+(a-b));break;
			case "*":
				System.out.println(a+c+b+"="+(a*b));break;
			case "/":
				System.out.println(a+c+b+"="+(a/b));break;
			}	
		}
		catch(InputMismatchException e){
			System.out.println("数据格式异常!");
		}
		catch(ArithmeticException e){
        	System.out.println("除零异常!");
        }
	}
}

运行结果:
(1)
在这里插入图片描述

(2) 在这里插入图片描述

  1. 设计一个程序,根据三角形的三边求三角形面积。要求自定义一个异常类IllegaException,在求面积的方法area()中声明抛出这个异常类型,当从键盘输入三条边的数据不能构成三角形时抛出异常。
    实验代码:
package test8;
import java.util.InputMismatchException;
import java.util.Scanner;

public class exp4 {

	public static void main(String[] args) throws IllegaException{
		// TODO Auto-generated method stub
		Scanner p=new Scanner(System.in);
		try{
			System.out.println("请输入三角形的三条边:");
			int a=p.nextInt();
			int b=p.nextInt();
			int c=p.nextInt();
			double S=area(a,b,c);
			System.out.println("三角形的面积:"+S);
		}
		catch(InputMismatchException e){
        	System.out.println("输入数据类型不匹配!");
        }
	}
public static double area(int a,int b,int c)throws IllegaException{
	if(a+b<=c||a+c<=b||b+c<=a||a<=0||b<=0||c<=0)
		throw new IllegaException();
	double s=(a+b+c)/2.0;
	double S=Math.sqrt(s*(s-a)*(s-b)*(s-c));
	return S;
}
}
class IllegaException extends Exception{
	IllegaException(){
		System.out.println("这三条边不能构成三角形");
	}
}

运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值