Exception 笔记
来源:互联网 发布:二次解析接口源码 编辑:程序博客网 时间:2024/05/17 00:14
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 mainf()java.lang.RuntimeException //输出异常全路径at exceptiontest.test.ExceptionTest1.f(ExceptionTest1.java:7) //跟上一个一样at exceptiontest.test.ExceptionTest1.main(ExceptionTest1.java:14)end main2)打印所有异常的方法
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 mainf()java.lang.RuntimeExceptionat exceptiontest.test.ExceptionTest1.f(ExceptionTest1.java:6)at exceptiontest.test.ExceptionTest1.main(ExceptionTest1.java:12)java.lang.RuntimeException //异常全路径null //e.getMessagenull //e.getLocalizedMessagenull //e.getCause[Ljava.lang.StackTraceElement;@184ec44 //e.getStackTraceend main3)初始化信息
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 mainf()java.lang.RuntimeException: 空指针异常 e.printStackTrace()打印的堆栈信息:后面多了messageat exceptiontest.test.ExceptionTest1.f(ExceptionTest1.java:6)at exceptiontest.test.ExceptionTest1.main(ExceptionTest1.java:12)Caused by: java.lang.NullPointerException 增加caused by 异常链... 2 morejava.lang.RuntimeException: 空指针异常 e.toString空指针异常空指针异常java.lang.NullPointerException e.getCauseexceptiontest.test.ExceptionTest1,ExceptionTest1.java,6,f className,fileName,行数,方法 exceptiontest.test.ExceptionTest1,ExceptionTest1.java,12,main end main
0 0
- Exception 笔记
- Exception 笔记
- boost exception 学习笔记
- java学习笔记-EXCEPTION
- php学习笔记--Exception
- java学习笔记--Exception
- 笔记-Testng FileNotFound Exception
- Java学习笔记之Exception
- Java 复习笔记 异常Exception
- Exception
- Exception
- Exception
- exception
- Exception
- Exception
- Exception
- exception
- Exception
- Ubuntu 远程桌面
- ndarray 矢量化(vectorization)
- 如何在linux下开启telnet服务
- Doxygen安装、中文PDF输出全攻略(Linux + MacOS)
- 二叉树先序遍历,中序遍历,后续遍历 非递归版本+二叉树转换为string保存+二叉树的层次遍历+平衡二叉树判断
- Exception 笔记
- 就业前的感言2016-7-28
- Hibernate的二级缓存管理
- Linux入门笔记——文件操作命令2
- linux 中切换用户:su和su -的使用环境变量详解
- Windows7下使用VS2015搭建Lua开发环境
- win10 无法登录远程桌面的解决方法
- 查看linux版本
- 图形学的日文博客或者链接