练习1:编写一个类,在其main()方法的try块里抛出一个Exception类的对象。传递一个字符串参数给Exception的构造器。在catch子句里捕获此异常对象,并且打印字符串参数。添加一个finally子句,打印一条信息以证明这里确实得到了执行。
package test12;
public class Ex01 {
public static void main(String[] args) {
try {
String s = "hello word!";
throw new Exception(s);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}finally {
System.out.println("finally block");
}
}
}
结果显示:
java.lang.Exception: hello word!
finally block
java.lang.Exception: hello word!
at test12.Ex01.main(Ex01.java:7)
Process finished with exit code 0
练习2:定义一个对象引用并初始化为null,尝试用此引用调用方法。把这个调用放在try-catch子句里以捕获异常。
package test12;
public class Ex02 {
private static Integer i = null;
public static void main(String[] args) {
try {
System.out.println(i.toString());
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
结果显示:
java.lang.NullPointerException
at test12.Ex02.main(Ex02.java:7)
Process finished with exit code 0
练习3:编写能产生并捕获ArrayIndexOutOfBoundsException异常的代码。
package test12;
public class Ex03 {
private static int[] ia = new int[2];
public static void main(String[] args) {
try {
ia[2] = 3;
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught ArrayIndexOutOfBoundsException");
e.printStackTrace();
}
}
}
结果显示:
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 2
at test12.Ex03.main(Ex03.java:8)
Process finished with exit code 0
练习4:使用extens关键字建立一个自定义异常类。为这个类写一个结束字符串参数的构造器,把此参数保存在对象内部的字符串引用中。写一个方法显示此字符串。写一个try-catch子句,对这个新异常进行测试。
package test12.Ex04;
class MyException extends Exception {
private String s;
public MyException(String s) {
super(s);
System.out.println("MyException()");
this.s = s;
}
protected void show() {
System.out.println("MyException + " + s );
}
}
public class Ex04{
public static void f() throws MyException {
System.out.println("f()");
throw new MyException("Exception from f()");
}
public static void main(String[] args) {
try {
f();
} catch (MyException e) {
System.out.println("Caught MyException");
e.printStackTrace();
e.show();
}
}
}
结果显示:
f()
MyException()
Caught MyException
MyException + Exception from f()
test12.Ex04.MyException: Exception from f()
at test12.Ex04.Ex04.f(Ex04.java:19)
at test12.Ex04.Ex04.main(Ex04.java:24)
Process finished with exit code 0
练习5:使用while循环建立类似“恢复模型”的异常处理行为,他将不断重复,直到异常不再抛出
package test12.Ex05;
public class Ex05 {
private static int[] ints = new int[3];
static int x = 5;
public static void main(String[] args) {
while (true) {
try {
ints[x] =1;
System.out.println(ints[x]);
System.out.println(x);
break;
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught ArrayIndexOutOfBoundsException");
e.printStackTrace();
x--;
}
}
}
}
结果显示:
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 5
at test12.Ex05.Ex05.main(Ex05.java:10)
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 4
at test12.Ex05.Ex05.main(Ex05.java:10)
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 3
at test12.Ex05.Ex05.main(Ex05.java:10)
1
2
Process finished with exit code 0
练习6:创建连个异常类,每一个都自动记录它们自己的日志,演示它们都可以正常运行。
package test12.Ex06;
import java.io.StringWriter;
import java.util.logging.Logger;
class MyException extends Exception {
private static Logger logger = Logger.getLogger("MyException");
static void logException(Exception e) {
StringWriter trace = new StringWriter();
e.printStackTrace();
logger.severe(trace.toString());
}
}
class MyException2 extends Exception {
private static Logger logger = Logger.getLogger("MyException2");
static void logException(Exception e) {
StringWriter trace = new StringWriter();
e.printStackTrace();
logger.severe(trace.toString());
}
}
public class Ex06 {
static void f() throws MyException {
throw new MyException();
}
static void g() throws MyException2 {
throw new MyException2();
}
public static void main(String[] args) {
try {
f();
} catch (MyException e) {
MyException.logException(e);
}
try {
g();
} catch (MyException2 e) {
MyException2.logException(e);
}
}
}
结果显示:
test12.Ex06.MyException
at test12.Ex06.Ex06.f(Ex06.java:26)
at test12.Ex06.Ex06.main(Ex06.java:34)
十二月 01, 2022 6:08:52 下午 test12.Ex06.MyException logException
严重:
test12.Ex06.MyException2
at test12.Ex06.Ex06.g(Ex06.java:29)
at test12.Ex06.Ex06.main(Ex06.java:39)
十二月 01, 2022 6:08:52 下午 test12.Ex06.MyException2 logException
严重:
Process finished with exit code 0
练习7:修改练习3,使得catch子句可以将结果作为日志记录。
package test12.Ex07;
import java.io.StringWriter;
import java.util.logging.Logger;
public class Ex07 {
private static int[] ia = new int[2];
private static Logger logger = Logger.getLogger("Ex07 Exception");
static void logException(Exception e) {
StringWriter trace = new StringWriter();
e.printStackTrace();
logger.severe(trace.toString());
}
public static void main(String[] args) {
try {
ia[2] = 3;
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught ArrayIndexOutOfBoundsException");
e.printStackTrace();
logException(e);
}
}
}
结果显示:
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 2
at test12.Ex07.Ex07.main(Ex07.java:18)
java.lang.ArrayIndexOutOfBoundsException: 2
at test12.Ex07.Ex07.main(Ex07.java:18)
十二月 01, 2022 6:22:23 下午 test12.Ex07.Ex07 logException
严重:
Process finished with exit code 0
练习8:定义一个类,令其方法抛出在练习2里定义的异常。不用异常说明,看看能否通过编译,然后加上异常说明,用try-catch子句测试该类和异常。
package test12.Ex08;
class Exception4 extends Exception {
private String msg;
Exception4(String msg) {
super(msg);
System.out.println("Exception");
this.msg = msg;
}
protected void showS() {
System.out.println("Message from Exception4" + msg);
}
}
class TestException {
public static void f() throws Exception4 {
System.out.println("f()");
throw new Exception4("Exception4");
}
}
public class Ex08 {
public static void main(String[] args) {
try {
TestException.f();
} catch (Exception4 e) {
System.err.println("Caught Exception4");
e.printStackTrace();
}
}
}
结果显示:
f()
Exception
Caught Exception4
test12.Ex08.Exception4: Exception4
at test12.Ex08.TestException.f(Ex08.java:20)
at test12.Ex08.Ex08.main(Ex08.java:27)
P