java的选择insofce_與Java中的Exception vs Throwable相關聯的開銷

I know

throw new Exception();

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

Does

有一個相當大的開銷,因為它創建一個完整的stackTrace等

throw new Throwable();

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

出現同樣的問題?這種行為是繼承的,還是拋出Throwable有一個較小的(o no)開銷?

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.

從分析師的角度來看,拋出UserNotValidException聽起來是正確的。如果您的代碼具有相當好的抽象,則返回null或0只是聽起來不正確。我只想知道我是否可以在代碼中實現這一點,或者如果我不得不將其留給理論。

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.

注意:我給出了一個非常簡單而愚蠢的例子,這不是我的情況。注2:我知道返回null將是普通的事情,但我需要有適當的抽象和OO代碼,而且,就個人而言,我認為沒有任何傷害。

8 个解决方案

#1

49

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

Throwable在創建時也會創建堆棧跟蹤。來自Throwable的java文檔:

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

throwable包含其創建時線程執行堆棧的快照。

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

因此,就創建堆棧跟蹤的開銷而言,Exception和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

37

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.

這會創建一個異常實例,它不會填充堆棧跟蹤(創建例外委托給fillInStackTrace來實際填充堆棧跟蹤),因此創建起來很便宜。

#3

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.

使用JIT編譯,實際上並不是在Java中拋出異常有很多被無意中聽到的情況。但投擲一個Throwable並沒有太大的不同,因為你也會得到一個堆棧跟蹤。

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.

如果您感興趣,有一篇非常有趣的論文稱為“即時編譯中的高效Java異常處理”(鏈接)。不是輕松閱讀,但信息量很大。

#4

1

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

你永遠不應該扔或抓住Throwable。例外的范圍太大了。

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.

如前所述,只有在需要時才應使用例外情況,即:在特殊情況下,應特別針對產生它們的情況。除此之外,捕獲Throwable意味着一系列異常,例如OutOfMemoryException。這種程度的錯誤無法從(輕松)恢復,並且不應由開發人員處理。

#5

1

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

Throwable是Exception的父類。所以Exception類繼承自Throwable。

#6

1

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.

您可以查看這兩個類的源代碼,看看除了暴露與Throwable相同的構造函數之外,Exception不會做任何事情。所有的肉,因此頭頂上,都生活在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.

即使Exception確實引入了一些額外的開銷,使用Throwable也會明顯過度優化。使用正確的工具來完成工作,不要因為它更輕而選擇錯誤的工具。

#7

0

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

java.lang.Exception擴展了java.lang.Throwable,因此它的開銷相同。來自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.

Throwable類是Java語言中所有錯誤和異常的超類。只有作為此類(或其子類之一)的實例的對象才被Java虛擬機拋出,或者可以被Java throw語句拋出。類似地,只有這個類或其子類之一可以是catch子句中的參數類型。

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).

兩個子類的實例Error和Exception通常用於表示發生了異常情況。通常,這些實例是在特殊情況的上下文中新創建的,以便包括相關信息(例如堆棧跟蹤數據)。

#8

0

4e4b6368ba3d397f88cec315dab83751.gif

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

正如@mangoDrunk所說:“Throwable是異常和錯誤的超類。”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值