Exception 笔记

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值