Overhead associated with Exception vs Throwable in Java

让人想犯罪 __ 提交于 2019-11-27 04:23:39

问题


I know

throw new Exception();

has a pretty large overhead, since it creates a full stackTrace, etc.
Does

throw new Throwable();

present the same problem? Is this behaviour inherited, or does throwing a Throwable has a smaller (o no) overhead?

EDIT
From an analyst point of view, a user inserting wrong password is an exception to the normal execution order of a program. So if I have:

public Session newSession() {  
  validate_user_and_password();   
}

throwing a UserNotValidException would sound correct from an analysts point of view.
Returning null or 0 just sounds incorrect if your code has pretty good abstraction. I just wanted to know if I could actually implement this in code, or if I'd have to just leave it to theory.

There's a good difference between programming-point-of-view exception and analyst-point-of-view exception.

Note: I've given a really simple and silly example, this is not quite my case.
Note 2: I know returning null would be the ordinary thing, but I'm required to have properly abstracted and OO code, and, personally, I see no harm in this.


回答1:


Throwable also creates a stacktrace when it's created. From the java docs for Throwable:

throwable contains a snapshot of the execution stack of its thread at the time it was created.

So in terms of overhead with regards to creating a stacktrace, there should be no difference between Exception and Throwable.

If you are using exceptions for "exceptional events" (as you should be), then you shouldn't be too concerned with the overhead of a stacktrace. An exceptional event occurs rarely in running code. So Exceptions shouldn't impact the performance of normal code in any significant way.




回答2:


Nope, you need your own subclass to avoid that effect.

Exception ex = new Exception() {
    @Override public Throwable fillInStackTrace() {
        return this; // and do nothing else
    }
};

This creates an instance of exception that will not fill the stack trace (the creation of exceptions delegates to fillInStackTrace to actually fill the stack trace) and is thus cheap to create.




回答3:


With JIT compilation, it is actually not still the case that there is a lot of overheard to throwing an Exception in Java. But throwing a Throwable is not much different, since you will get a stack trace there as well.

If you are interested, there is a very interesting paper called "Efficient Java exception handling in just-in-time compilation" (link). Not a light read, but quite informative.




回答4:


You should never be throwing or catching Throwable. The scope of the exception is far too great.

As stated previously, exceptions should be used only where needed, ie: in exceptional circumstances and should be specific to the situation that spawned them. That aside, catching a Throwable implies a host of exceptions, such as OutOfMemoryException. An error of this magnitude can not be recovered from (easily) and should not be handled by the developer.




回答5:


Throwable is parent class of Exception. so Exception class is inherited from Throwable.




回答6:


Java Exception

As @mangoDrunk said:"Throwable is the superclass of exception and error."




回答7:


You can look at the source code of the two classes to see that Exception doesn't do anything beyond expose the same constructors as Throwable. All of the meat, and hence overhead, lives in Throwable.

Even if Exception did introduce some additional overhead it would be a clear over-optimization to use Throwable instead. Use the right tool for the job, don't co-opt the wrong tool just because it's lighter.




回答8:


java.lang.Exception extends java.lang.Throwable, so it's the same overhead. From the Javadoc:

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

Instances of two subclasses, Error and Exception, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).



来源:https://stackoverflow.com/questions/2129647/overhead-associated-with-exception-vs-throwable-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!