实验八 异常处理

实验八  异常处理

实验目的

  1. 了解异常的概念和异常处理机制
  2. 掌握捕捉异常的方法
  3. 掌握创建自定义异常

实验学时 2学时

实验内容

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

package code81;
import java.util.InputMismatchException;  //导入输入不匹配异常
import java.util.Scanner;
public class code81 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		input:
		while(true) {  //循环输入
		try {  //选定捕获异常的范围
			Scanner in = new Scanner(System.in);  
			double r,area;
			System.out.println("请输入圆的半径:");
			r=in.nextDouble();
			area=r*r*3.14;
			System.out.println("圆的面积为:"+area);
		  }catch(InputMismatchException e) {  //处理输入不匹配异常
			  System.out.println("输入的数据不符合要求!");
			  System.out.println("请重新输入");
			  e.printStackTrace();  //在命令行打印异常信息在程序中出错的位置及原因
			  continue input;  //持续输入,确保输入的数据正确
		  }
		  }
	}

}

2.分析下面的程序。

(1)程序在运行时会产生哪些异常?怎样捕获并处理异常?

(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;

    float average;

              n=sc.nextInt();

              for(int i=0;i<n;i++){

                 a[i]=sc.nextInt();

                 sum=sum+a[i];

              }

              average=(float)sum/n;

              System.out.println(average);

      }

}

  1. 可能会产生输入的数据不匹配异常、除0异常、数组越界异常;
package code82;
import java.util.Scanner;
import java.util.InputMismatchException;  //导入输入不匹配异常

public class ExceptionSample1 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		int a[]=new int[5];
		int n,sum=0;
		float average;
		n=sc.nextInt();
		for(int i=0;i<n;i++) {
			try {  //选定捕获异常的范围
			a[i]=sc.nextInt();
			sum=sum+a[i];
			average=(float)sum/n;
			System.out.println(average);
		}catch(InputMismatchException e) {  //处理输入不匹配异常
			System.out.println("输入的数据格式不符合要求!");
		}catch(ArithmeticException e) {  //处理除0异常
			System.out.println("除0异常!");
		}catch(ArrayIndexOutOfBoundsException e) {  //处理数组越界异常
			System.out.println("数组越界异常!");
		}
		}
	}
}

2.修改代码:不管程序在执行过程中会不会产生异常,最后都输出“程序运行结束”

package code82;
import java.util.Scanner;
import java.util.InputMismatchException;  //导入输入不匹配异常
public class ExceptionSample2 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		int a[]=new int[5];
		int n,sum=0;
		int average;
		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) {  //处理除0异常
			System.out.println("除0异常!");
		}finally {  //保证最后程序都按要求输出
			System.out.println("程序运行结束!");
		}

	}

}

3.设计一个程序,输入两个整数和一个算术运算符(+、-、*、/),根据运算符,计算两个整数的运算结果。考虑到用户输入的数据不合法,需要捕获异常(数据格式异常、除零异常、非法运算符异常)。

package code83;
import java.util.Scanner;
import java.util.InputMismatchException;  //导入输入不匹配异常
public class Calculate {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner in = new Scanner(System.in);
		int a,b;
		String c;
		try {  //选定捕获异常的范围
			System.out.println("请输入需要计算的两个整数:");
			a=in.nextInt();
			b=in.nextInt();
			System.out.println("请输入运算符:");
			c=in.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;
			default:  
				throw new Exception();  //声明抛弃异常
			}
		}catch(InputMismatchException e) {  //处理数据不匹配异常
			System.out.println("数据格式异常!");
		}catch(ArithmeticException e) {  //处理除0异常
			System.out.println("除0异常!");
		}catch(Exception e) {  //处理default语句中抛弃的非法运算符异常
			System.out.println("非法运算符异常!");
		}
			

	}

}

4.设计一个程序,根据三角形的三边求三角形面积。要求自定义一个异常类IllegaException,在求面积的方法area()中声明抛出这个异常类型,当从键盘输入三条边的数据不能构成三角形时抛出异常。

package code84;
import java.util.Scanner;
import java.util.InputMismatchException;  //导入输入不匹配异常
public class Triangle {
	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 area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
			return area;  //求三角形的面积
		}
	public static void main(String[] args) throws IllegaException {
		// TODO 自动生成的方法存根
		Scanner in = new Scanner(System.in);
		int a,b,c;
		System.out.println("请输入三角形的三边长:");
		try {  //选定捕获异常的范围
			a=in.nextInt();
			b=in.nextInt();
			c=in.nextInt();
			System.out.println("三角形的面积为:"+area(a,b,c));  //调用求面积的方法,输出面积的值
		}catch(InputMismatchException e) {  //处理输入不匹配异常
			System.out.println("输入的数据格式不匹配!");
		}

	}

}
package code84;

public class IllegaException extends Exception {  //自定义异常类IllegaException
	IllegaException(){  //构造方法
		System.out.println("这样的三边不能构成一个三角形!");
	}

}

实验小结

  1. 使用异常处理比传统的错误管理技术更具优势;
  2. 使用异常处理可以将错误处理代码与正常代码分离;
  3. 如果一个方法并不知道如何处理所出现的异常,则可以在声明该方法时声明抛弃该异常;
  4. 除了运行时异常以外的继承于Exception类的子类都统称为非运行时异常,对于这类异常,Java编译器要求程序必须捕获或者声明抛弃异常;
  5. 捕获异常:try...catch...finally;
  6. 不论try语句块中是否有异常发生,finally语句块都会被执行;
  7. 如果Java提供的系统异常类型不能满足程序设计的需要,就可以设计自己的异常类型。Java推荐用户的异常类型以Exception为直接父类。
  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文文喜欢郭子吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值