异常抛出e.printStackTrace();工作原理
来源:互联网 发布:oracle sql优化工具 编辑:程序博客网 时间:2024/05/16 17:49
//测试类
public class TestPrintStackTrace {
public static void main(String[] args) {try {
f();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void f() throws Exception {
throw new Exception("出问题啦!");
}
}
//查看e.printStackTrace();源码
public class Throwable implements Serializable{
//因为System.err是PrintStream类型,所以调用了public void printStackTrace(PrintStream s)函数
public void printStackTrace() {
printStackTrace(System.err);
}
//此处的s=System.err,构造了WrappedPrintStream(s)后直接走private void printStackTrace(PrintStreamOrWriter s)
public void printStackTrace(PrintStream s) {
printStackTrace(new WrappedPrintStream(s));
}
//这里就是e.printStackTrace()实现的底层代码,有些看不懂没事,抓住主干代码看,看他如何抛出异常的
private void printStackTrace(PrintStreamOrWriter s) {
// Guard against malicious overrides of Throwable.equals by
// using a Set with identity equality semantics.
Set<Throwable> dejaVu = Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
dejaVu.add(this);
synchronized (s.lock()) {
// Print our stack trace
s.println(this);//此处的this是mian方法中的new Exception("出问题啦!"),所以此处默认调用toString方法
/*public String toString() {
String s = getClass().getName();
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
}*/
//toString方法中s=文件名,message=出问题啦!
//形如:
//此处便是用for循环输出每个出错位置,getOurStackTrace()获取每个出错的位置复制给trace,然后用增强for遍历,到此就是全部过程
StackTraceElement[] trace = getOurStackTrace();
for (StackTraceElement traceElement : trace)
s.println("\tat " + traceElement);
// Print suppressed exceptions, if any
for (Throwable se : getSuppressed())
se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu);
// Print cause, if any
Throwable ourCause = getCause();
if (ourCause != null)
ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu);
}
}
//WrappedPrintStream类
private static class WrappedPrintStream extends PrintStreamOrWriter {
private final PrintStream printStream;
//此处的printStream=System.err
WrappedPrintStream(PrintStream printStream) {
this.printStream = printStream;
}
Object lock() {
return printStream;
}
void println(Object o) {
printStream.println(o);
}
}
}
- 异常抛出e.printStackTrace();工作原理
- e.printStackTrace()
- e.printStackTrace()
- e.printStackTrace()
- e.printStackTrace() ;
- 【java知识点异常】e.getMessage()与e.printStackTrace()的区别
- 使用e.printStackTrace(PrintWriter s)对异常信息处理
- 使用e.printStackTrace(PrintWriter s)对异常信息处理
- 使用e.printStackTrace(PrintWriter s)对异常信息处理
- getInstanceCount and e.printStackTrace
- JAVA Catch中异常信息的输出 System.out.println(e) e.printStackTrace()
- 异常e.printStackTrace();这个方法与e.getMessage();有什么区
- 【java知识点异常】throw new RuntimeException(e)与e.printStackTrace( )的区别
- java 异常printStackTrace
- 怎样把e.printStackTrace()这个异常保存到一个字符串变量中如:String str;
- 怎样把e.printStackTrace()这个异常保存到一个字符串变量中如:String str;
- 怎样把e.printStackTrace()这个异常保存到一个字符串变量中如:String str;
- 在Android中用e.printStackTrace()
- 超级台阶
- azkaban源码解读
- It怎样从基层做起
- android字体和可免费商用的字体
- 添加nginx为系统服务(service nginx start/stop/restart)
- 异常抛出e.printStackTrace();工作原理
- centos java mysql的安装(2)
- 破解算法
- 简述 OAuth 2.0 的运作流程
- HDOJ2049(错排+组合)
- tomcat启动报错,logs/catalina.out:Could not find or load main class org.apache.catalina.startup.Bootstr
- 数据库学习笔记2(补遗笔记1)
- js中function(){}中小括号中的内容到底是什么
- Elasticsearch之四种查询类型和搜索原理