• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java code execution and try-catch blocks  RSS feed

 
abhi narkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We had a big debate in our team regarding the try-catch block and any even if negligible performance impact that may arise because of the number of statements in a try block. The question is - is there any performance impact or overhead on JVM due to any code included in the try block? A point made was that JVM while executing the code in a try block always checks against the exception that is caught in catch there by taking more time than it would take to execute a statement that is not inside a try block - is this true?
I am still searching for answers on net and books in library but haven't been able to find anything related or something that answers this question. Thanks for the help.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding:

The try block is a block that if an exception is thrown, then Java has a tie to the catch blocks and searches through the blocks trying to find the corresponding catch. If there is no exception thrown, then there really isn't any overhead with the try block.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhi narkar wrote:We had a big debate in our team regarding the try-catch block and any even if negligible performance impact that may arise because of the number of statements in a try block...

You can write a simple POC and run that several times to know yourself.
Just write two methods having some 100 statements each.
In first method, put those statements in a for loop and iterate for a huge number.
In second method, put those statements in a try/catch block and put the try/catch block in a for loop and iterate those number of times.
Note down the time taken to complete the loop in both cases.
Run both methods several times and you will come to know the truth.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And post your code and observations here.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhi narkar wrote:if negligible performance impact that may arise because of the number of statements in a try block. The question is - is there any performance impact or overhead on JVM due to any code included in the try block?

Two slightly different questions: I suspect (but I'm not sure) that there is some overhead to putting statements in a try block, but I doubt if the number of statements makes any difference.

I'm also almost certain that there is an overhead for try...catch, because I seem to remember reading somewhere that the compiler can't do some of the optimizations (like statement re-ordering) that it can in open code; but I'm not so sure about try on its own or even try...finally.

Easiest, as Tapas suggested: Suck it and see.

Winston
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran tests for this, and there is basically NO DIFFERENCE that I can detect after 1/2 billion iterations.

Winston Gutkowski wrote:
abhi narkar wrote:if negligible performance impact that may arise because of the number of statements in a try block. The question is - is there any performance impact or overhead on JVM due to any code included in the try block?

Two slightly different questions: I suspect (but I'm not sure) that there is some overhead to putting statements in a try block, but I doubt if the number of statements makes any difference.

I'm also almost certain that there is an overhead for try...catch, because I seem to remember reading somewhere that the compiler can't do some of the optimizations (like statement re-ordering) that it can in open code; but I'm not so sure about try on its own or even try...finally.

Easiest, as Tapas suggested: Suck it and see.

Winston
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:I ran tests for this, and there is basically NO DIFFERENCE that I can detect after 1/2 billion iterations.

Same with me...
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you use the javap tool to inspect the bytecode and you can see how the catch is implemented in bytecode.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!