简体   繁体   English

比较Java中已检查和未检查的异常(性能,基准)

[英]Comparing checked and unchecked exceptions (Performance, Benchmarks) in Java

Are there scenarios where you could argue that when an exception is created that both a checked exception and an unchecked exception can be used? 在某些情况下,您可能会争辩说,在创建异常时可以同时使用已检查的异常和未检查的异常吗? Then the speed and performance can be measured against each other 然后可以相互衡量速度和性能

From the perfomance point of view, building stack trace for exception is anyway the longest operation when an exception is generated ( details ). 从性能的角度来看,生成异常的堆栈跟踪无论如何都是生成异常时最长的操作( 详细信息 )。

Checked and unchecked exceptions make difference only at compile time. 已检查和未检查的异常仅在编译时有所不同。 The Java compiler forces you to either catch checked exceptions or declare them in the method signature. Java编译器迫使您要么捕获检查的异常,要么在方法签名中声明它们。 It was supposed to improve program safety, but the majority opinion seems to be that it's not worth the design problems it creates. 本来可以提高程序安全性,但是大多数人似乎认为这样做不值得产生设计上的问题。

There is even Lomboks "magical" @SneakyThrows annotation to overcome this compile-time check. 甚至还有Lomboks“ magical” @SneakyThrows批注来克服此编译时检查。 On the JVM (class file) level, all exceptions, checked or not, can be thrown regardless of the throws clause of your methods, which is why this works. 在JVM(类文件)级别上,无论方法的throws子句如何,都可以引发所有已检查或未检查的异常,这就是为什么这样做的原因。

As the others already said, there is no performance difference between checked and unchecked exceptions. 正如其他人已经说过的那样,已检查和未检查的异常之间没有性能差异。

But if you think that the performance of throwing or handling exceptions might be important for your application, you're doing something terribly wrong . 但是,如果您认为抛出或处理异常的性能对于您的应用程序可能很重要,那么您所做的事情就非常错误

Exceptions are meant to tell your caller that your method could not fulfill its task. 异常是要告诉您的调用者您的方法无法完成其任务。 If this happens so often that it affects your program's overall performance, you have a bigger problem than just the performance - you have a non-functional program! 如果这种情况经常发生,从而影响到程序的整体性能,那么您将面临的不仅仅是一个性能问题,而是更大的问题-您的程序无法正常运行!

Always throw exceptions when you find a method can't fulfill its task, and catch exceptions only in places where you can reasonably continue even after some unspecified failure of some method nested deeply in your call stack (that's what the exception tells you) - typically, that's some top level (like eg the menu bar action of a desktop app). 当您发现某个方法无法完成其任务时,请务必抛出异常,并且仅在即使您的调用堆栈中深深嵌套的某些方法发生未指定的故障后,也可以在合理地继续执行的地方捕获异常(这就是异常告诉您的信息)-通常,这是最高级的(例如,桌面应用程序的菜单栏操作)。 To save you from writing lots of throws clases, unchecked exceptions can be helpful - but that's opinion-based. 为了避免编写大量的抛出异常,未经检查的异常可能会有所帮助-但这是基于观点的。

Throwing an exception is very expensive because you need to calculate all of your stack traces, because of it often helps with performance just return null/-1/etc. 抛出异常非常昂贵,因为您需要计算所有堆栈跟踪,因为它通常仅返回null / -1 / etc即可帮助提高性能。 Or you could use the constructor of Exception without writableStackTrace. 或者,您可以使用不带writableStackTrace的Exception的构造函数。 it's the last parameters in the constructor. 它是构造函数中的最后一个参数。 https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

protected Exception(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)

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

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