java 如果类型为 Throwable,如何查找异常原因

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6860378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:37:22  来源:igfitidea点击:

How to find cause of exception if type is Throwable

javainheritancetypes

提问by Zombies

Give this method here:

在这里给出这个方法:

public SomeClass(Throwable stackTrace) {
    super();
    this.stackTrace = stackTrace;
}

How can I find out what class type stacktraceoriginally was before being passed in?

如何stacktrace在传入之前找出最初的类类型?

回答by nfechner

stacktrace.getClass().getName()

or

或者

stacktrace instanceof CLASS_YOU_WANT_TO_TRY

回答by Mike Yockey

You make this determination by handling exceptions as explicitly as possible. That includes not ever, EVER, catching Throwableand handling exceptions as near to the code that threw it as possible. I'm not sure what you're trying to do by constructing a POJO with a Throwablebut it's probably not a path that will lead you to much enlightenment.

您可以通过尽可能明确地处理异常来做出此决定。这包括永远,永远Throwable尽可能接近抛出异常的代码捕获和处理异常。我不确定你想通过用 a 构建一个 POJO 来做什么,Throwable但这可能不是一条能让你获得很多启发的途径。