第七章作业

1. 不对

2.

class SelfException extends Exception{
	public SelfException(String msg) {
		super(msg);
	}
}
public class Test {
	public static void main(String[] args) throws SelfException{
		int a = 1;
		if(a == 1)
			throw new SelfException("a == 1");
	}
}
3.
  1、使用了未初始化的变量(虽然已经声明)   2、使用了未初始化的对象(虽然已经声明)   3、使用了关键字或已存在的类名作变量对象方法或类名。

4.

exception000
finally111
finished

finally111
exception

finished

exception
finished


每个异常只会被捕捉一次。

5.

13423

6.

class Dummy{
	int data;
    Dummy(){
		data = 0;
	}
}
public class Test {
	public static void badMethod() {
		try {
			Dummy d = null;
			System.out.println(d.data); // 对象未初始化
		}catch(NullPointerException np) {
			System.out.println("空指针异常");
		}catch(ArithmeticException at) {
			System.out.println("除数为零");
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args){
		badMethod();
	}
}
7.

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
	public static boolean isNumerric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if(isNum.matches())
			return true;
		return false;
	}
	public static void main(String[] args){
		int count = 10;
		int min = Integer.MAX_VALUE;
		int max = Integer.MIN_VALUE;
		Scanner in = new Scanner(System.in);
		while(count > 0) {
			System.out.println("input number: ");
			String str = in.next();
			if(isNumerric(str)) {
				int num = Integer.parseInt(str);
				if(min > num) min = num;
				if(max < num) max = num;
				--count;
			}else {
				System.out.println("输入错误");
			}
		}
		System.out.println("max: " + max + "\nmin: " + min);
		in.close();
	}
}
8.

throws TimeOutException





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值