Java异常处理实验

Posted 康小庄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java异常处理实验相关的知识,希望对你有一定的参考价值。

1.从键盘输入一个整形数n,如果输入正确的话,输出10-n后的值,如果输入错误的话输出“not int”最后输出end。

public class ScannerErrors 

	public static void main(String[] args) 
		Scanner sc = new Scanner(System.in);
		try 
			int m = Integer.parseInt(sc.next());
			System.out.println(10 - m);
		 catch (NumberFormatException e) 
			System.out.println("not int");
		 finally 
			System.out.println("end");
		
		sc.close();
	


2.模拟向货船上装载集装箱,每个集装箱有一定重量,货船总重大于每个集装箱,装载若干集装箱后,如果货船超重,那么货船认为这是一个异常,将拒绝装载集装箱,但无论是否发生异常,货船都需要正点启航。

CargoBoat

public class CargoBoat 
	private int realcontent;
	private int maxcontent;

	public int getRealcontent() 
		return realcontent;
	

	public void setRealcontent(int realcontent) 
		this.realcontent = realcontent;
	

	public int getMaxcontent() 
		return maxcontent;
	

	public void setMaxcontent(int maxcontent) 
		this.maxcontent = maxcontent;
	

	public void loading(int m) throws DangerException 
		this.realcontent += m;
		if (realcontent > maxcontent) 
			throw new DangerException();
		
		System.out.printf("目前装载了%d吨的货物\\n", realcontent);
	

DangerException

public class DangerException extends Exception 
	public void showError() 
		System.out.println("超重!");
	

TestDangerException

public class TestDangerException 

	public static void main(String[] args) 
		CargoBoat cargoBoat = new CargoBoat();
		Scanner sc = new Scanner(System.in);
		cargoBoat.setMaxcontent(100);
		int m = 0;
		try 
			while (true) 
				System.out.println("请输入装载箱重量,当前装载箱重量为" + cargoBoat.getMaxcontent() + "吨");
				m = sc.nextInt();
				cargoBoat.loading(m);
			
		 catch (DangerException e) 
			e.showError();
			System.out.printf("无法再装载重量是%d吨的集装箱\\n", m);
		 finally 
			System.out.printf("启航出发!");
		
		sc.close();

	


3.写一个方法void triangle(int a,int b,int c),判断三个参数是否能构成一个三角形。如果不能则抛出异常IllegalArgumentException,显示异常信息:“a,b,c不能构成三角形”;如果可以构成则显示三角形三个边长。在主方法中得到命令行输入的三个整数,调用此方法,并捕获异常。 (注:两边之和大于第三边:a+b>c;两边之差小于第三边:a>c-a)

ErrorsTriangle

public class ErrorsTriangle 
	public static void triangle(int a, int b, int c) throws Exception 
		if (a + b > c && c - a < b) 
			System.out.println("能构成三角形");
			System.out.println("a=" + a + "," + "b=" + b + "," + "c=" + c);
		 else 
			throw new IllegalArgumentException("a=" + a + "," + "b=" + b + "," + "c=" + c + "不能构成三角形");
		
	

	public static void main(String[] args) 
		int a[] = new int[3];
		System.out.println("输入三个整数并用,分隔");
		Scanner sc = new Scanner(System.in);
		String[] strings = sc.nextLine().split(",");

		for (int i = 0; i < strings.length; i++) 
			a[i] = Integer.parseInt(strings[i]);
		
		Arrays.sort(a);
		try 
			triangle(a[0], a[1], a[2]);
		 catch (IllegalArgumentException e) 
			e.printStackTrace();
		 catch (Exception e) 
			e.printStackTrace();
		
	

4.编写整除运算程序,要求捕获除数为0异常、数字格式异常、通用型异常。注意要把通用型异常的捕获顺序放在最后。
5.把第4题整除程序改为双精度型实数的除法运算程序,并更改有关提示信息,运行该程序若干次,每次输入不同的数据,观察分析程序的运行结果。
6.在第4题基础上编写自定义异常类(必须继承系统的Exception类),在除数为0时抛出自定义异常,并捕获处理该异常

public class DivisibleOperation 

	public static void main(String[] args) 
		System.out.println("输入整型除数和被除数,形如a/b");
		Scanner sc = new Scanner(System.in);
		String[] strings = sc.nextLine().split("/");
		int a = Integer.parseInt(strings[0]);
		int b = Integer.parseInt(strings[1]);
		System.out.println(a + "/" + b + "的结果是" + divide(a, b));
	

	public static int divide(int a, int b) 
		if (b == 0) 
			throw new ArithmeticException("除数不能为0");
		
		return a / b;
	

DoubleOperation

public class DoubleOperation 

	public static void main(String[] args) 
		System.out.println("输入双精度除数和被除数,形如a/b");
		Scanner sc = new Scanner(System.in);
		String[] strings = sc.nextLine().split("/");
		double a = Double.parseDouble(strings[0]);
		double b = Double.parseDouble(strings[1]);
		System.out.println(a + "/" + b + "的结果是" + divide(a, b));
	

	public static double divide(double a, double b) 
		if (b == 0) 
			throw new ArithmeticException("除数不能为0");
		
		return a / b;
	

CustomException

public class CustomException extends Exception 
	public void error() 
		System.out.println("被除数不能为0");
	

TestCustomException

public class TestCustomException 

	public static void main(String[] args) 
		System.out.println(divide());
	

	public static double divide() 
		Scanner input = new Scanner(System.in);
		double a, b;
		for (;;) 
			try 
				System.out.println("输入第一个整数!");
				a = input.nextDouble();
				System.out.println("输入第二个整数!");
				b = input.nextDouble();
				if (b == 0) 
					throw new CustomException();
				
				break;
			 catch (CustomException e) 
				e.error();
				System.out.println("重新输入!");
			
		
		return a / b;
	

以上是关于Java异常处理实验的主要内容,如果未能解决你的问题,请参考以下文章

Java:异常处理

Java 异常处理

异常处理

Java异常处理

JAVA 异常处理机制

Java 异常处理