package exceptiontest.test;
public class ExceptionTest1 {
public static void f(){
System.out.println("f()");
throw new RuntimeException();
}
public static void main(String[] args) {
System.out.println("start main");
f();
}
}
虚拟机捕捉到异常,这个是由main方法抛出:执行过程为:运行start main,调用f方法,打印f(),在下一行抛出异常,main没有捕获,由main抛出,虚拟机捕获打印
Exception in thread "main" java.lang.RuntimeException //
at exceptiontest.test.ExceptionTest1.f(ExceptionTest1.java:7) //出错地点 at + 类全路径.方法名(类型名.java:行数)
at exceptiontest.test.ExceptionTest1.main(ExceptionTest1.java:14) //上一个出错地点
捕捉异常之后处理
1)打印异常,不会终止程序
package exceptiontest.test;
public class ExceptionTest1 {
public static void f(){
System.out.println("f()");
throw new RuntimeException();
}
public static void main(String[] args) {
System.out.println("start main");
try{
f();
}catch(Exception e){
e.printStackTrace();//打印异常
}
System.out.println("end main");
}
}
执行过程:输出start main 调用f,输出f(),抛出异常,捕获,打印异常堆栈,输出end main
start main
f()
java.lang.RuntimeException //输出异常全路径
at exceptiontest.test.ExceptionTest1.f(ExceptionTest1.java:7) //跟上一个一样
at exceptiontest.test.ExceptionTest1.main(ExceptionTest1.java:14)
end main
2)打印所有异常的方法
package exceptiontest.test;
public class ExceptionTest1 {
public static void f(){
System.out.println("f()");
throw new RuntimeException();
}
public static void main(String[] args) {
System.out.println("start main");
try{
f();
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
System.out.println(e.getMessage());
System.out.println(e.getLocalizedMessage());
System.out.println(e.getCause());
System.out.println(e.getStackTrace());
}
System.out.println("end main");
}
}
输出结果:
start main
f()
java.lang.RuntimeException
at exceptiontest.test.ExceptionTest1.f(ExceptionTest1.java:6)
at exceptiontest.test.ExceptionTest1.main(ExceptionTest1.java:12)
java.lang.RuntimeException //异常全路径
null //e.getMessage
null //e.getLocalizedMessage
null //e.getCause
[Ljava.lang.StackTraceElement;@184ec44 //e.getStackTrace
end main
3)初始化信息
package exceptiontest.test;
public class ExceptionTest1 {
public static void f(){
System.out.println("f()");
throw new RuntimeException("空指针异常",new NullPointerException());
}
public static void main(String[] args) {
System.out.println("start main");
try{
f();
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
System.out.println(e.getMessage());
System.out.println(e.getLocalizedMessage());
System.out.println(e.getCause());
for(StackTraceElement element : e.getStackTrace()){
System.out.println(element.getClassName()+","+element.getFileName()+","+element.getLineNumber()+","+element.getMethodName());
}
}
System.out.println("end main");
}
}
输出结果:
start main
f()
java.lang.RuntimeException: 空指针异常 e.printStackTrace()打印的堆栈信息:后面多了message
at exceptiontest.test.ExceptionTest1.f(ExceptionTest1.java:6)
at exceptiontest.test.ExceptionTest1.main(ExceptionTest1.java:12)
Caused by: java.lang.NullPointerException 增加caused by 异常链
... 2 more
java.lang.RuntimeException: 空指针异常 e.toString
空指针异常
空指针异常
java.lang.NullPointerException e.getCause
exceptiontest.test.ExceptionTest1,ExceptionTest1.java,6,f className,fileName,行数,方法
exceptiontest.test.ExceptionTest1,ExceptionTest1.java,12,main
end main