performance - Overhead of rethrowing Java exception vs. using instanceof/cast -


I know that the overhead of Java exceptions has been done to death on SO, but I did not know anything Was addressing my situation. I have a future, which can put an execution exception () on the call, in which any application-specific exception occurs. I was wondering if it is an important overhead to use the effort-catching block rather than the example-then-cast pattern. For example, this might look like this:

  Private response handle exception (execution exception e) throws my excuse to remove {try {throw e.getCause (); } Hold (ApplicationException1 e1) {// known error MyApplicationException.convert (e1); } Hold (ApplicationException2 e2) {// Create error response Return new response (E2); } Hold (Throne T) {// Unknown Error New Runtime Exception (T) Throwing; }} Personal feedback handle exception 2 (execution exception e) MyApplicationException throws {throwable cause = e.getCause (); If (ApplicationException1 for example) {ApplicationException1 e1 = (ApplicationException1) reason; Throw MyApplicationException.convert (e1); } And if (applicationException2 for example) {ApplicationException2 e2 = (ApplicationException2) reason; Returned new returns (E2); } And {new runtime exception (cause); }}  

My theory is that

  • Exception and stack trace have already been created.
  • I am referring to the exception object in both ways, either way.
  • Exception is immediately caught and never promoted.

As a matter of style, I usually not recommend using the exception handler for regular control flow: div Class = "post-text" itemprop = "text">

I can see the logic of using it here, however, you need to open the original exception for the design of future .

Releasing exceptions should be less expensive than throwing a new exception, because the stack trace has already been populated. With your first approach, it can still be higher, but if your application is throwing so many exceptions that the effect becomes impossible, then you may have major problems.

If this is a concern for you, the way you will get a meaningful answer, the difference is for measuring yourself but, again, exception only extraordinary cases Should be thrown into; They should be unusual from the design even if you double the cost of exception handling, then the cost is 2n instead of n if you throw so many exceptions There is a clear demonstration of your application, so a simple factor of two is probably not going to make or break you. So use whatever style you have more readable.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -