1.前言:
你的程序总有一天会崩溃掉,在崩溃掉的时候我们要知道它在哪,为了什么而崩溃掉,数据的保存或者丢失情况如何等问题。我们可以通过继承类java.lang.Throwable的子类:Exception来设计我们自己的Java异常。Exception类用于描述程序能够捕获的异常,如ClassNotFoundException。要注意的是自定义异常类之间也可以有继承关系,同时也需要为自定义异常类设计构造方法,以方便构造自定义异常对象。
2.设计实例分析:
这是个比较完整的自定义异常类的设计,其实是比较模板化的东西。
package playground;
import java.io.*;
public class MyException extends Exception {
private int id; // a unique id
private String classname; // the name of the class
private String method; // the name of the method
private String message; // a detailed message
private MyException previous =
null; // the exception which was caught
private String separator = "\n"; // line separator
public MyException(int id, String classname, String method,
String message, MyException previ