简体   繁体   English

为什么java.lang.Throwable不是一个抽象类?

[英]why isn't java.lang.Throwable an abstract class?

Possible duplicate: why-is-java-lang-throwable-a-class 可能重复: why-is-java-lang-throwable-a-class

Hi! 嗨! I doesn't understand why Throwable isn't abstract class. 我不明白为什么Throwable不是抽象类。 I see only one use case for these: in logging systems for figure out call hierarchy. 我只看到一个用例:在用于计算调用层次结构的日志记录系统中。 But it can be some static method for this or other class. 但它可以是这个或其他类的一些静态方法。 So, why?) 所以为什么?)

Thanks. 谢谢。

upd UPD

from java.util.logging.LogRecord 来自java.util.logging.LogRecord

// Get the stack trace.
StackTraceElement stack[] = (new Throwable()).getStackTrace();

Why it can't be Throwable.getStackTrace(); 为什么它不能是Throwable.getStackTrace(); or as in java.lang.Thread 或者像在java.lang.Thread中一样

(new Exception()).getStackTrace();

In this way we can avoid throw new Throwable(); 通过这种方式我们可以避免throw new Throwable();

upd2 from javadoc 来自javadoc的upd2

The Throwable class is the superclass of all errors and exceptions in the Java language. Throwable类是Java语言中所有错误和异常的超类。

So, as a superclass it should be abstract, imho. 所以,作为一个超类,它应该是抽象的,imho。 Using it for getting stacktrace isn't good case by this definition. 使用它来获取堆栈跟踪不是这个定义的好例子。

I doesn't understand why Throwable isn't abstract class. 我不明白为什么Throwable不是抽象类。

The answer is clearly stated here . 这里清楚地说明答案。

Why it can't be Throwable.getStackTrace(); 为什么它不能是Throwable.getStackTrace(); or as in java.lang.Thread 或者像在java.lang.Thread中一样

Quite simply, the getStackTrace() calls the getOurStackTrace() method which is non-static. 很简单, getStackTrace()调用非静态的getOurStackTrace()方法。 If getStackTrace() was static, so should getOurStackTrace() . 如果getStackTrace()是静态的,那么getOurStackTrace() This won't happen as printStackTrace() method uses the getOurStackTrace() . 这不会发生,因为printStackTrace()方法使用getOurStackTrace() This is elaborated in the JavaDoc: 这在JavaDoc中详细说明:

Provides programmatic access to the stack trace information printed by printStackTrace(). 提供对printStackTrace()打印的堆栈跟踪信息的编程访问。

Source for java.lang.Throwable: java.lang.Throwable的来源:

 public StackTraceElement[] getStackTrace() {
        return (StackTraceElement[]) getOurStackTrace().clone();
    }

Also, if you read the code of getOurStackTrace() method, you'll see it calls the following method: 此外,如果您阅读 getOurStackTrace()方法的代码,您将看到它调用以下方法:

 
 
 
  
  private native int getStackTraceDepth();
 
  

As far as I know, native cannot be static (I may be wrong). 据我所知, native不能是静态的(我可能是错的)。

I use it quite often for logging, so I am glad it isn't abstract. 我经常使用它来记录,所以我很高兴它不是抽象的。 There is a method to get the call stack, Thread.currentThread().getStackTrace() but this returns a StackTraceElement[] which isn't very useful for logging. 有一种获取调用堆栈的方法,Thread.currentThread()。getStackTrace()但是这会返回一个StackTraceElement [],这对于日志记录来说并不是很有用。

EDIT: 编辑:

StackTraceElement[] stes = Thread.currentThread().getStackTrace();  

Note: this method also works to get a stack trace of another thread which can be handy. 注意:此方法也可用于获取另一个可以方便的线程的堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM