Java面向对象程序设计第7章1-8
1.“程序中凡是可能出现异常的地方必须进行捕获或拋出”,这句话对吗?
不对。
异常分两类,runtime异常和非runtime异常。
- runtime异常,比如NullPointException等,这一类你不在程序里面进行try/catch,编译不会出错。
- 非runtime异常,比如SqlException等或自定义的exception,这一类在程序里不进行try/catch或throws,编译就会出错。
2.自定义一个异常类,并在程序中主动产生这个异常类对象。
class SelfGenerateException extends Exception {
SelfGenerateException(String msg) {
super(msg); //调用Exception的构造方法
}
static void throwOne() throws SelfGenerateException {
int a = 1;
if (a == 1) {
//如果a为1就认为在特定应用下存在异常,改变执行路径,抛出异常
throw new SelfGenerateException("a为1");
}
}
public static void main(String args[]) {
try {
throwOne();
} catch (SelfGenerateException e) {
e.printStackTrace();
}
}
}
结果:
SelfGenerateException: a为1
at SelfGenerateException.throwOne(test.java:10)
at SelfGenerateException.main(test.java:16)
3.借助JDK帮助,请列举发生NullPointerException异常的一些情况。
JDK解释如下:
当应用程序试图在需要对象的地方使用 null 时,抛出该异常。这种情况包括:
调用 null 对象的实例方法。
访问或修改 null 对象的字段。
将 null 作为一个数组,获得其长度。
将 null 作为一个数组,访问或修改其时间片。
将 null 作为 Throwable 值抛出。
应用程序应该抛出该类的实例来指示其他对 null 对象的非法使用。
4.不执行程序,指出下面程序的输出结果;如果将黑体代码去掉,写出输出结果;如果再将斜体代码去掉,写出输出结果。
public class test
{
public static void aMethod() throws Exception{
try{
throw new Exception();
}
//------------------黑体------------------------------------------------
catch(Exception e){
System.out.println("exception000");
}
//----------------------------------------------------------------------
//--------------------斜体-----------------------------------------------
finally{
System.out.println("exception111");
}
//---------------------------------------------------------------------------
}
public static void main(String[] args){
try{
aMethod();
}
catch(Exception e){
System.out.println("exception");
}
System.out.println("finished");
}
}
输出:
exception000
exception111
finished
去黑体输出:
exception111
exception
finished
如果再将斜体代码去掉:
Error:(4, 9) java: 'try' 不带有 'catch', 'finally' 或资源声明
就是说try不能单独存在而不带有catch或者finally声明
再去掉aMthod里的try后
输出:
exception
finished
5.不执行程序,指出下面程序的输出结果。
public class test{
public static String output ="";
public static void foo(int i){
try{
if(i==1) {throw new Exception();}
output += "1";
}
catch(Exception e){
output += "2";
return;
}
finally{output += "3";}
output += "4";
}
public static void main(String args[]){
foo(0);
foo(1);
System.out.println(test.output);
}
}
结果:13423
解释:134是foo(0)输出的,23是foo(1)输出的。foo(1)catch里return后,也会执行finally,但是finally之后的东西就不在执行了。foo(0)不会执行return语句,所以可以执行finally后面的语句
6. 编写一个程序方法,对空指针异常、除数为零异常给出出错的中文提示。当有新异常发生时,可扩展该方法中的代码进行统一处理。
public class test {
public static void main(String[] args) {
try {
String s = null;
System.out.println(s.charAt(0));//空指针异常
//System.out.println(1/0);//除零异常
} catch (NullPointerException e) {
System.out.println("空指针异常");
} catch (ArithmeticException e) {
System.out.println("计算异常");
} catch (Exception e) {
System.out.println("其他异常");
e.printStackTrace();
}
}
}
7.从屏幕输入10个数,在输入错误的情况下,给出相应的提示,并继续输人。在输入完成的情况下,找到最大最小数。
import java.util.Arrays;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
int index = 0;
while (true) {
try {
arr[index] = sc.nextInt();
if(index!=9)
index++;
else{
break;
}
} catch (Exception e) {
sc.next();
System.out.println("输入类型有误,请重新输入");
}
}
Arrays.sort(arr);
System.out.println("max=" + arr[index]);
System.out.println("min=" + arr[0]);
}
}
8.阅读下面程序,TimedOutException为自定义异常,完成指定方法后面的部分。
public void method()throws TimedOutException
{
success= connect();
if(success == -1)
{
throw new TimedOutException();
}
}