Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?

Considering you have code like this:

doSomething() // this method may throw a checked a exception //do some assignements calculations doAnotherThing() //this method may also throw the same type of checked exception //more calls to methods and calculations, all throwing the same kind of exceptions. 

Now I know, there is in fact a performance hit when constructing the exception, specifically unwinding the stack. And I have also read several articles pointing to a slight performance hit when entering try/catch blocks, but none of the articles seem to conclude anything.

My question is, is it recommended to keep the lines inside the try catch to bare minimum?, i.e. ONLY have inside the try clause the lines that can actually throw the exception you are catching. Does the code inside the try clause run slower or cause any performance hit?.

But more important what it is the best practice/more readable solution considering doing this:

try {     doSomething() // this method may throw a checked a exception //do some assignements calculations doAnotherThing() //this method may also throw the same type of checked exception //more calls to methods and calculations, all throwing the same kind of exceptions. } catch (MyCheckedException e) {    //handle it } 

or :

try {     doSomething() // this method may throw a checked a exception } catch (MyCheckedException e) {    //Store my exception in a Map (this is all running in a loop and I want it to   continue running, but I also want to know which loops didn't complete and why)    continue;      }   //do some assignements calculations try {     doAnotherThing() // this method may throw a checked a exception } catch (MyCheckedException e) {     //Store my exception in a Map (this is all running in a loop and I want it to   continue running, but I also want to know which loops didn't complete and why)    continue; }  

This is considering that you will handle ALL this checked exceptions exactly the same way of course.

like image 729
Oscar Gomez Avatar asked Nov 25 '10 21:11

Oscar Gomez


People also ask

What should you put in a try-catch?

Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.

Is it good to use try-catch in Java?

try/catch clause is used for things that goes wrong that are outside of your control and not in the normal program flow. For example, trying to write to a file and the file system is full? That situation should typically be handled with try/catch .

Is it mandatory to use catch after try in Java?

Nope, not at all. Its not mandatory to put catch after try block, unless and until the try block is followed by a finally block. Just remember one thing, after try, a catch or a finally or both can work.

What is the usage of try and catch clause in Java?

Java try and catchThe try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.


1 Answers

Is it recommended to keep the lines inside the try catch to bare minimum?

No. Can't imagine how you could think that the length of a try block or indeed of any block can have any impact on performance.

Does the code inside the try clause run slower or cause any performance hit?.

No.

As you observed, exceptions only incur performance costs when thrown.

If you're concerned about 'try' performance, surely the thing to do is keep the code inside to a maximum?

like image 170
user207421 Avatar answered Sep 22 '22 16:09

user207421