Java异常处理 如何跟踪异常的传播路径
当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。
可使用printStackTrace 和 getMessage方法了解异常发生的情况:
printStackTrace:打印方法调用堆栈。
每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。
示例程序
// UsingExceptions.java // Demonstrating the getMessage and printStackTrace // methods inherited into all exception classes. public class PrintExceptionStack { public static void main( String args[] ) { try { method1(); } catch ( Exception e ) { System.err.println( e.getMessage() + "\n" ); e.printStackTrace(); } } public static void method1() throws Exception { method2(); } public static void method2() throws Exception { method3(); } public static void method3() throws Exception { throw new Exception( "Exception thrown in method3" ); } }
结果截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Java线程并发工具类CountDownLatch原理及用法
这篇文章主要介绍了Java线程并发工具类CountDownLatch原理及用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-10-10java对象list使用stream根据某一个属性转换成map的3种方式举例
开发小伙伴们通常会需要使用到对象和Map互相转换的开发场景,下面这篇文章主要给大家介绍了关于java对象list使用stream根据某一个属性转换成map的3种方式,需要的朋友可以参考下2024-01-01spring boot项目application.properties文件存放及使用介绍
这篇文章主要介绍了spring boot项目application.properties文件存放及使用介绍,我们的application.properties文件中会有很多敏感信息,大家在使用过程中要多加小心2021-06-06SpringMVC源码解读之HandlerMapping - AbstractUrlHandlerMapping系列re
这篇文章主要介绍了SpringMVC源码解读之HandlerMapping - AbstractUrlHandlerMapping系列request分发 的相关资料,需要的朋友可以参考下2016-02-02MyBatis测试报错:Cannot determine value type from string &a
这篇文章主要给大家介绍了关于MyBatis测试报错:Cannot determine value type from string 'xxx'的解决办法,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2023-02-02String StringBuilder StringBuffer区别以及源码分析
string是C++标准库的一个重要的部分,主要用于字符串处理。可以使用输入输出流方式直接进行string操作,同时,C++的算法库对string类也有着很好的支持,并且string类还和c语言的字符串之间有着良好的接口2021-06-06
最新评论