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
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