一、Exception 类
Exception 类继承于java.lang.Throwable 类,且没有增加自己的方法,所以只需了解Throwable 类。
Throwable 类所有public 方法:
public Throwable implements Serializable{
//构造方法
public Throwable();
public Throwable(String message);//包含提示信息
public Throwable(String message, Throwable cause);
public Throwable(Throwable cause);
//实例方法
public String getMessage();
public String getLocalizedMessage();//与getMessage相同
public synchronized Throwable getCause();
public synchronized Throwable initCause(Throwable cause);//设置导致此异常的异常
public String toString();//打印错误信息
public void printStackTrace();//以System.err打印栈信息
public void printStackTrace(PrintStream s);
public void printStackTrace(PrintWriter s);
public synchronized Throwable fillInStackTrace();
public StackTraceElement[] getStackTrace();
public void setStackTrace(StackTraceElement[] stackTrace);
public final synchronized void addSuppressed(Throwable exception);//附带一个异常
public final synchronized Throwable[] getSuppressed();
}
为直观理解,简单实践:
public class StudyErrorAndException {
public static void main(String[] args) {
Exception e = new Exception("异常");
e.addSuppressed(new Exception("suppressed异常"));
e.initCause(new Exception("cause异常"));
e.printStackTrace();
}
}
实践结果:
java.lang.Exception: 异常
at StudyErrorAndException.main(StudyErrorAndException.java:6)
Suppressed: java.lang.Exception: suppressed异常
at StudyErrorAndException.main(StudyErrorAndException.java:7)
Caused by: java.lang.Exception: cause异常
at StudyErrorAndException.main(StudyErrorAndException.java:8)
Exception 类定义:
public class Exception extends Throwable {
static final long serialVersionUID = -3387516993124229948L;
public Exception() {
super();
}
public Exception(String message) {
super(message);
}
public Exception(String message, Throwable cause) {
super(message, cause);
}
public Exception(Throwable cause) {
super(cause);
}
protected Exception(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);//writableStackTrace创建时是否打印异常信息
}
}
自定义异常类:
class MyException extends Exception{
private static final long serialVersionUID = 1L;
public MyException(){
super();
}
public MyException(String msg){
super(msg);
}
public MyException(String msg,Throwable deliverException){
super(msg);
this.addSuppressed(deliverException);
}
public MyException(String msg,Throwable cause,boolean writeStackTrack){
super(msg,cause,false,writeStackTrack);
}
}
注:Throwable 类除了四个参数的构造方法可以控制创建时打印错误异常信息外,其它构造方法都是默认不打印栈错误信息的。重在学会通过构造方法设置cause 异常,通过addSuppressed(Throwable t) 包含其他异常。
二、Error 类
同样继承于java.lang.Throwable 类,与Exception 类的定义形式完全相同,只是意义上的差别。Error 指无法容忍的错误,通常指系统或硬件错误,而非代码中的错误,代码中不需处理,它不需在方法声明处throws,同时它不会在编译时进行检查,而Exception 会。所以,普通程序代码中都只是用Exception 而非Error。Error 也可捕获,如:
public class StudyErrorAndException {
public static void main(String[] args) {
try{
throw new MyError("错误");
}catch(Exception ex){
System.out.println("Exception occurs");
}catch(MyError er){
System.out.println("Error occurs");
}finally{
System.out.println("最后处理");
}
}
}
class MyError extends Error{
private static final long serialVersionUID = 1L;
public MyError(String msg){
//msg:提示信息;第二个参数:cause异常;第三个:是否允许suppressed异常;第四个:创建时打印错误
super(msg,null,true,true);
}
}
实践结果:
Error occurs
最后处理
Error 即使设置构造方法的第四个参数为true,也不会打印错误信息,所以打印错误必须调用printStackTrack([...])。
三、try-catch 与finally 语句
实践:
public class StudyErrorAndException {
public static void main(String[] args) {
System.out.println(devide(1,0));
}
@SuppressWarnings("finally")
private static int devide(int fz,int fm){
int rs;
try{
return fz/fm;
}catch(Exception ex){
System.out.println("Exception occurs");
rs = Integer.MAX_VALUE;
return rs;
}finally{
rs = -1;
// return rs;
}
}
}
结果:
1、需要在finally 中返回结果时,需要加@SuppressWarning("finally")注解,此时catch 中的return 语句无效;
2、若不在finally 中返回,则finally 中对返回变量的修改不会影响catch 返回的值(即上面随翻finally 中将rs 赋值为-1,但catch 中并不返回-1).
四、assert 断言语句
断言语句在调试代码阶段非常有用,一般用于程序不准备通过捕获异常来处理的错误。在代码调试阶段让断言语句发挥作用,这样就可以发现一些致命的错误,当程序正式运行时,关闭断言语句但仍让其保留,可用于以后可能的再次调试中。
Java解释器直接运行应用程序时,默认关闭断言语句,在调试程序时可以用-ea启用断言语句,如:java -ea mianClass。
断言语句格式:
assert booleanExpression;
assert booleanExpression:messageException;
说明:
第一种格式,booleanExpression是求值为boolean值得表达式,当值为true时,继续执行,否则停止执行;
第二种格式,会在结果区输出包含messageException(字符串表达式)的信息,用于提示出现了怎样的问题。形式如:
assert 0>1:"0不大于1";
将提示:
Exception in thread "main" java lang AssertionError:0不大于1,at ***.main(***.java:7)