- All your lab reports should be uploaded to BB before the deadline.
- Must be original works, to prohibit any copying or plagiarism
- to learn exception and exception handling
- to learn the advantage of exception
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?
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?
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、- ArithmeticException
- ArrayIndexOutOfBoundsException
- StringIndexOutOfBoundsException
- ClassCastException
- NullPointerException
- 输出Infinity
- statement1和statement2正常执行后就可以执行statement3
- 会执行
- 依然执行
该程序在执行到的时候会引发ArrayIndexOutOfBoundsException,检查该Exception的结构可以看见
然后检查ArithmeticException,可以看见
显然这里会抛出RuntimeException
4、这里调用方法,方法内出现1/0的整数除法异常,throws出ArithmeticException被外try-catch捕获并输出ArithmeticException
5、这里方法内出现了数组下标越界的错误,即ArrayIndexOutOfBoundsException并被try-catch以父类RuntimeException捕获,因此方法外并不报错,故外try-catch正常执行
6、- statement4、statement1都会执行
- statement4、statement1都会执行
- statement4会执行,statement5不会执行
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("输入有误");
}
}
}
}



