栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ZUCC

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ZUCC

Lab Report 12 Note:
  • All your lab reports should be uploaded to BB before the deadline.
Caution
  • Must be original works, to prohibit any copying or plagiarism
一、Experimental Purposes and Requirements
  • to learn exception and exception handling
  • to learn the advantage of exception
二、Experimental Contents 1 、What RuntimeException will the following programs throw, if any?
public class Test {
	public static void main(String[] args) {
		System.out.println(1 / 0);
	}
}
public class Test {
	public static void main(String[] args) {
		int[] list = new int[5];
		System.out.println(list[5]);
	}
}
public class Test {
	public static void main(String[] args) {
		String s = "abc";
		System.out.println(s.charAt(3));
	}
}
public class Test {
	public static void main(String[] args) {
		Object o = new Object();
		String d = (String) o;
	}
}
public class Test {
	public static void main(String[] args) {
		Object o = null;
		System.out.println(o.toString());
	}
}
public class Test {
	public static void main(String[] args) {
		System.out.println(1.0 / 0);
	}
}
2 、Suppose that statement2 causes an exception in the following try-catch block:
try {
	statement1;
	statement2;
	statement3;
}catch (Exception1 ex1) {
}catch (Exception2 ex2) {
}
statement4;

Answer the following questions (Use the given Exercise12_2 .java to analize):

  • Will statement3 be executed?
  • If the exception is not caught, will statement4 be executed?
  • If the exception is caught in the catch block, will statement4 be executed?
3、 What is displayed when the following program is run?Why?
class Test {
	public static void main(String[] args) {
		try {
			int[] list = new int[10];
			System.out.println("list[10] is " + list[10]);
		} catch (ArithmeticException ex) {
			System.out.println("ArithmeticException");
		} catch (RuntimeException ex) {
			System.out.println("RuntimeException");
		} catch (Exception ex) {
			System.out.println("Exception");
		}
	}
}
4、 What is displayed when the following program is run?Why?
class Test{
	public static void main(String[] args) {
		try {
			method();
			System.out.println("After the method call");
		} catch (ArithmeticException ex) {
			System.out.println("ArithmeticException");
		} catch (RuntimeException ex) {
			System.out.println("RuntimeException");
		} catch (Exception e) {
			System.out.println("Exception");
		}
	}
	static void method() throws Exception {
		System.out.println(1 / 0);
	}
}
5、What is displayed when the following program is run?Why?
class Test {
	public static void main(String[] args) {
		try {
			method();
			System.out.println("After the method call");
		} catch (RuntimeException ex) {
			System.out.println("RuntimeException in main");
		} catch (Exception ex) {
			System.out.println("Exception in main");
		}
	}
	static void method() throws Exception {
		try {
			String s = "abc";
			System.out.println(s.charAt(3));
		} catch (RuntimeException ex) {
			System.out.println("RuntimeException in method()");
		} catch (Exception ex) {
			System.out.println("Exception in method()");
		}
	}
}
6、Suppose that statement2 causes an exception in the following statement:
try {
	statement1;
	statement2;
	statement3;
}catch (Exception1 ex1) {
}finally {
	statement4;
}
statement5;

Answer the following questions(Use the give Exercise12_06.java to analyze):

  • If no exception occurs, will statement4 be executed, and will statement5 be executed?
  • If the exception is of type Exception1, will statement4 be executed, and will statement5 be executed?
  • If the exception is not of type Exception1, will statement4 be executed, and will statement5 be executed?
7、(InputMismatchException) Write a program that prompts the user to read two integers and displays their multiplication. Your program should prompt the user to read the number again if the input is incorrect.

Hint: you can refer to the book code Listing 12.5.
Test example 1:

Test example 2:

8 、(ArrayIndexOutOfBoundsException) Write a program that meets the followingrequirements:
  • Creates an array with 100 randomly chosen integers.
  • prompts the user to enter the index of the array, then displays the corresponding element value. If the specified index is out of bounds, display the message Out of Bounds.

Test example 1:

Test example 2:

三、 Please show your questions analysis, code and results. 1、
  1. ArithmeticException
  2. ArrayIndexOutOfBoundsException
  3. StringIndexOutOfBoundsException
  4. ClassCastException
  5. NullPointerException
  6. 输出Infinity
2、
  1. statement1和statement2正常执行后就可以执行statement3
  2. 会执行
  3. 依然执行
3、

该程序在执行到的时候会引发ArrayIndexOutOfBoundsException,检查该Exception的结构可以看见

然后检查ArithmeticException,可以看见

显然这里会抛出RuntimeException

4、

这里调用方法,方法内出现1/0的整数除法异常,throws出ArithmeticException被外try-catch捕获并输出ArithmeticException

5、

这里方法内出现了数组下标越界的错误,即ArrayIndexOutOfBoundsException并被try-catch以父类RuntimeException捕获,因此方法外并不报错,故外try-catch正常执行

6、
  1. statement4、statement1都会执行
  2. statement4、statement1都会执行
  3. statement4会执行,statement5不会执行
7、
import java.util.Scanner;



public class InputMismatchExceptionTest {
    public static void main(String[] args) {
        int x, y;
        System.out.print("Enter two integers: ");
        while (true) {
            try {
                Scanner scanner = new Scanner(System.in);
                x = scanner.nextInt();
                y = scanner.nextInt();
                System.out.println("Multiplication is " + x * y);
                break;
            } catch (Exception e) {
                System.out.println("Incorrect input and re-enter two integers: ");
            }
        }
    }
}


c### 8、

import java.util.Scanner;



public class ArrayIndexOutOfBoundsExceptionTest {
    public static void main(String[] args) {
        int[] x = new int[100];
        for (int i = 0; i < 100; i++)
            x[i] = (int)(Math.random()*10000);
        while (true) {
            try {
                System.out.print("Enter an index:");
                Scanner scanner = new Scanner(System.in);
                int search = scanner.nextInt();
                System.out.println("The element is " + x[search]);
                break;
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Index out of bound");
            } catch (Exception e) {
                System.out.println("输入有误");
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530794.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号