创建自定义异常
1.在程序中,可能会遇到JDK提供的任何标准异常类都无法充分描述清楚我们想要表达的问题,这种情况下可以创建自己的异常类,即自定义异常类。
2.自定义异常类只需从Exception类或者它的子类派生一个子类即可。
3.自定义异常类如果继承Exception类,则为受检查异常,必须对其进行处理;如果不想处理,可以让自定义异常类继承运行时异常RuntimeException类。
4.习惯上,自定义异常类应该包含2个构造器:一个是默认的构造器,另一个是带有详细信息的构造器。
class MyException extends Exception{
public MyException() {}
public MyException(String msg) { super(msg);}
}
public class FullConstructors {
public static void f() throws MyException{
System.out.println("Throwing MyException from f()");
throw new MyException();
}
public static void g() throws MyException{
System.out.println("Throwing MyException from g()");
throw new MyException("Originated in g()");
}
public static void main(String[] args){
try{
f();
}catch (MyException e){
e.printStackTrace(System.out);
}
try{
g();
}catch (MyException e){
e.printStackTrace(System.out);
// 打印“从方法调用处直到异常抛出处”的方法调用序列。这里,信息被发送到了,并自动地被捕获和显示在输出中。
// e.printStackTrace(); 信息将被输出到标准错误流
}
}
}
/* Output:
Throwing MyException from f()
MyException
at FullConstructors.f(FullConstructors.java:9)
at FullConstructors.main(FullConstructors.java:17)
Throwing MyException from g()
MyException: Originated in g()
at FullConstructors.g(FullConstructors.java:13)
at FullConstructors.main(FullConstructors.java:22)
*/
异常与记录日志
使用java.util.logging工具将输出记录到日志中。
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.*;
class LoggingException extends Exception{
private static Logger logger= Logger.getLogger("LoggingException");
public LoggingException() {
StringWriter trace = new StringWriter();
printStackTrace(new PrintWriter(trace));
logger.severe(trace.toString());
}
}
public class LoggingExceptions {
public static void main(String[] args){
try{
throw new LoggingException();
}catch (LoggingException e){
System.err.println("Caught "+e);
}
try{
throw new LoggingException();
}catch (LoggingException e){
System.err.println("Caught "+e);
}
}
}
/* Output:
Dec 16, 2019 4:53:19 PM LoggingException <init>
SEVERE: LoggingException
at LoggingExceptions.main(LoggingExceptions.java:17)
Caught LoggingException
Dec 16, 2019 4:53:19 PM LoggingException <init>
SEVERE: LoggingException
at LoggingExceptions.main(LoggingExceptions.java:22)
Caught LoggingException
*/
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.*;
public class LoggingExceptions2 {
private static Logger logger= Logger.getLogger("LoggingException2");
static void LogException(Exception e){
StringWriter trace = new StringWriter();
e.printStackTrace(new PrintWriter(trace));
logger.severe(trace.toString());
}
public static void main(String[] args){
try{
throw new NullPointerException();
}catch (NullPointerException e){
LogException(e);
}
}
}
/* Output:
Dec 16, 2019 4:59:49 PM LoggingExceptions2 LogException
SEVERE: java.lang.NullPointerException
at LoggingExceptions2.main(LoggingExceptions2.java:23)
*/
如果要记录catch块中所发生异常,最好不要手动解析堆栈跟踪并将输出发送到 System.err(),而是使用java.util.logging包中的日志记录工具将输出发送到文件。
try {
Handler handler = new FileHandler("OutFile.log");
Logger.getLogger("").addHandler(handler);
} catch (IOException e) {
Logger logger = Logger.getLogger("package.name");
StackTraceElement elements[] = e.getStackTrace();
for (int i = 0, n = elements.length; i < n; i++) {
logger.log(Level.WARNING, elements[i].getMethodName());
}
}