1.不对。
2.
public class yichang {
public static void main(String[] args) {
System.out.print("Please input an Integer: ");
int value = new Scanner(System.in).nextInt();
try{
if(value < 0){
throw new MyException("My Exception comes!");
}
}catch (MyException myExp){
System.out.println("MyExcepton caugth!");
}
}
}
class MyException extends Exception{
public MyException(String s){
System.out.println("MyException occurs. The value must be greater than ZERO!");
}
}
3.当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括:调用 null 对象的实例方法。访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
4.输出: exception000
exception111
finished
去黑体输出:exception111
exception
finished
去斜体输出:exception000
finished
5.输出结果:13423
6.
public static void main(String[] args) {
// 输入计算参数
Integer a = 2;
Integer b = 1;
try {
double cc = b/(double)a; //这样才能计算小数,不会报除0错误
System.out.println("cc:"+cc);
} catch (NullPointerException e) {
System.out.println("空指针异常");
}catch (ArithmeticException e) {
System.out.println("计算异常");
}catch (Exception e) {
System.out.println("其他异常");
e.printStackTrace();
}
}
7.
import java.util.Scanner;
public class Input {
public static void main(String[] args) {
int index = 0;
int[] array = new int[10];
int max,min; while (true) {
if (index == array.length) { break;}
Scanner sc = new Scanner(System.in);
System.out.print("输入[" + (index + 1) + "]:");
try {
array[index] = sc.nextInt();
index++;
}
catch (Exception e) {System.out.println("输入错误,重新输入!");}
}
max = array[0];
min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {max = array[i];}
if (array[i] < min) {min = array[i];}
}
System.out.println("max = " + max);
System.out.println("min = " + min);
}
}
8.throws TimeOutException