12.2.1
使用异常处理的优势是:将检测错误(由被调用的方法完成)从处理错误(由调用方法完成)中分离出来。
12.2.2
下面哪些语句会抛出异常?
(1).System.out.println(1/0); 抛出异常
(2).System.out.println(1.0/0); Infinity(无穷)
在Java中,浮点有无限值和NaN的概念。(NaN(Not a Number,非数)是计算机科学中数值数据类型的一类值,表示未定义或不可表示的值。常在浮点数运算中使用。首次引入NaN的是1985年的IEEE 754浮点数标准。)(百度出来的)
12.2.3
指出下面代码的问题。代码会抛出异常吗。
long value =Long.MAX_VALUE+1;
System.out.println(value);
超出long的范围了。没有抛出异常,输出-9223372036854775808
12.2.4
当产生一个异常时,JVM会做什么?如何捕获一个异常。
JVM(Java Virtual Machine,Java虚拟机).如何捕获。。。超出范围了。。。
12.2.5
下面代码的输出是什么?
public class Test{
public static void main(String[] args){
try{
int value=30;
if(value<40)
throw new Exception("value is too small");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
System.out.println("Continue after the catch block");
}
}
value is too small
Continue after the catch block
把30换成50
Continue after the catch block
12.2.6
给出下面代码的输出
public class Test{
public static void main(String[] args){
for(int i=0;i<2;i++){
System.out.print(i+" ");
try{
System.out.println(1/0);
}
catch(Exception ex){
}
}
}
}
输出 0 1
public class Test{
public static void main(String[] args){
try{
for(int i=0;i<2;i++)
{
System.out.print(i+" ");
System.oout.print(1/0);
}
}
catch(Exception ex){
}
}
}
Exception in thread “main” java.lang.Error: Unresolved compilation problem: