第七章

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值