7

第七章课后作业

不对。
异常分两类,runtime异常和非runtime异常。
runtime 异常,这一类不在程序里面进行try catch,编译不会出错。(隐式声明抛出)
非runtime异常,比如自定义的exception,这一类在程序里不进行try/catch或throws,编译就会出错。
2.

public class SelfGenerateException extends Exception
{
	SelfGenerateException(String msg){
		super(msg);   //调用Exception的构造方法
	}
	static void throwOne() throws SelfGenerateException
	{
		int a = 1;
		if (a==1) 
		{throw new SelfGenerateException("a为1");}		
	}
	public static void main(String args[])
	{
		try
		{throwOne();}
		catch(SelfGenerateException e)
	    {e.printStackTrace();}
	}
}

课本上的
3.
NullpointerException:
调用 null 对象的实例方法。
某个声明没有赋以对象就调用方法。
访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
数组尚未分配空间就开始使用。

public class TestArray {
	 private static int[] x;
	 public  static void main(String[] args) {
		System.out.println(x[0]);
	 }
 }

将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
4.
1:
exception000
exception111
finished
2:
exception111
exception
finished
3:
exception000
finished
5.
13423
6.

public class T{
	public static void main(String[] args) {
        try {
        	String s = null;
            System.out.println(1/0);//除零异常
            System.out.println(s.charAt(0));//空指针异常
        }catch (NullPointerException e) {
            System.out.println("空指针异常");
        }catch (ArithmeticException e) {
            System.out.println("计算异常");
        }catch (Exception e) {
            System.out.println("其他异常");
            e.printStackTrace();
        }
    }


import java.util.Scanner;//输入

public class T {
    private static Scanner sc;

	public static void main(String[] args) {
          int dex = 0;
          int[] arr = new int[10];
          int max,min;
          while (dex<arr.length) {
                
                sc = new Scanner(System.in);
                System.out.print("输入" + (dex + 1) + ":");
                try {
                     arr[dex] = sc.nextInt();
                     dex++;
                 } 
                 catch (Exception e) {System.out.println("输入错误,重新输入!");}
           }
           max = arr[0];
           min = arr[0];
           for (int i = 1; i < arr.length; i++) {
                 if (arr[i] > max) {max = arr[i];}
                 if (arr[i] < min) {min = arr[i];}
            }
           System.out.println("max = " + max);
           System.out.println("min = " + min);
          
     }
}

throws TimedOutException

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值