views:

6068

answers:

18

I have a loop that looks something like this:

for(int i = 0; i < max; i++) {
    String myString = ...;
    float myNum = Float.parseFloat(myString);
    myFloats[i] = myNum;
}

This is the main content of a method whose sole purpose is to return the array of floats. I want this method to return null if there is an error, so I put the loop inside a try...catch block, like this:

try {
    for(int i = 0; i < max; i++) {
        String myString = ...;
        float myNum = Float.parseFloat(myString);
        myFloats[i] = myNum;
    }
} catch (NumberFormatException ex) {
    return null;
}

But then I also thought of putting the try...catch block inside the loop, like this:

for(int i = 0; i < max; i++) {
    String myString = ...;
    try {
        float myNum = Float.parseFloat(myString);
    } catch (NumberFormatException ex) {
        return null;
    }
    myFloats[i] = myNum;
}

So my question is: is there any reason, performance or otherwise, to prefer one over the other?


EDIT: The consensus seems to be that it is cleaner to put the loop inside the try/catch, possibly inside its own method. However, there is still debate on which is faster. Can someone test this and come back with a unified answer? (EDIT: did it myself, but voted up Jeffrey and Ray's answers)

+2  A: 

In your examples there is no functional difference. I find your first example more readable.

Jamie
A: 

I don't know about the performance, but the code looks cleaner with the try catch outside the for loop.

Jack B Nimble
+30  A: 

Performance: as Jeffrey said in his reply, in Java it doesn't make much difference.

Generally, for readability of the code, your choice of where to catch the exception depends upon whether you want the loop to keep processing or not.

In your example you returned upon catching an exception. In that case, I'd put the try/catch around the loop. If you simply want to catch a bad value but carry on processing, put it inside.

The third way: You could always write your own static ParseFloat method and have the exception handling dealt with in that method rather than your loop. Making the exception handling isolated to the loop itself!

class Parsing
{
    public static Float MyParseFloat(string inputValue)
    {
        try
        {
            return Float.parseFloat(inputValue);
        }
        catch ( NumberFormatException e )
        {
            return null;
        }
    }

    // ....  your code
    for(int i = 0; i < max; i++) 
    {
        String myString = ...;
        Float myNum = Parsing.MyParseFloat(myString);
        if ( myNum == null ) return;
        myFloats[i] = (float) myNum;
    }
}
Ray Hayes
Look closer. He's exiting on the first exception in both. I thought the same thing too at first.
kervin
I was aware, that's why I said in his case, since he's returning when he hits a problem then I'd use an outside capture. For clarity, that's the rule I'd use...
Ray Hayes
Nice code, but unfortunately Java doesn't have the luxury of out parameters.
Michael Myers
ByRef? Been so long since I've touched Java!
Ray Hayes
No, pretty much the only way to do this would be to wrap the float as a property of an object. Then inside ParseFloat, you'd set outputValue.myFloat instead of setting outputValue directly. But this is getting ugly in a hurry.
Michael Myers
Changed to using a Float which is either null or the float value you want!
Ray Hayes
Ah, not a bad idea. But we're kind of getting away from the question, aren't we? Although I guess this does make the loop itself prettier.
Michael Myers
Someone else suggested TryParse which in C#/.net allows you to do a safe parse without dealing with exceptions, that's now replicated and the method is reusable. As for the original question, I'd prefer the loop inside the catch, it just looks cleaner even if there is no performance issue..
Ray Hayes
All right, fair enough. I'm not necessarily going to accept your answer (I'm going to give it a day or so to let the answers settle), but I'll probably come back and up-vote you.
Michael Myers
+1  A: 

You should prefer the outer version over the inner version. This is just a specific version of the rule, move anything outside the loop that you can move outside the loop. Depending on the IL compiler and JIT compiler your two versions may or may not end up with different performance characteristics.

On another note you should probably look at float.TryParse or Convert.ToFloat.

Orion Adrian
A: 

If it's inside, then you'll gain the overhead of the try/catch structure N times, as opposed to just the once on the outside.


Every time a Try/Catch structure is called it adds overhead to the execution of the method. Just the little bit of memory & processor ticks needed to deal with the structure. If you're running a loop 100 times, and for hypothetical sake, let's say the cost is 1 tick per try/catch call, then having the Try/Catch inside the loop costs you 100 ticks, as opposed to only 1 tick if it's outside of the loop.

Stephen Wrighton
Can you elaborate a little on the overhead?
Michael Myers
elaboration added
Stephen Wrighton
Okay, but Jeffrey L Whitledge says there's *no* extra overhead. Can you provide a source that says there is?
Michael Myers
The cost is small, almost negligible, but it's there--everything has a cost. Here's a blog article from Programmer's Heaven (http://tiny.cc/ZBX1b) concerning .NET and here's a newsgroup post from Reshat Sabiq concerning JAVA (http://tiny.cc/BV2hS)
Stephen Wrighton
I see... So now the question is, is the cost incurred on entering the try/catch (in which case it should be outside the loop), or is it constant for the duration of the try/catch (in which case it should be inside the loop)? You say it's #1. And I'm still not entirely convinced there *is* a cost.
Michael Myers
According to this Google Book (http://tinyurl.com/3pp7qj), "the latest VMs show no penalty".
Michael Myers
The Java link above doesn't indicate whether the performance penalty results from calling a method with exception handling or executing a loop with exception handling. The method call does have a performance penalty, which will be the same in both scenarios given in the question.
Jeffrey L Whitledge
A: 

If you put the try/catch inside the loop, you'll keep looping after an exception. If you put it outside the loop you'll stop as soon as an exception is thrown.

Joel Coehoorn
Well, I'm returning null on exception, so it wouldn't keep processing in my case.
Michael Myers
Ah, missed the return.
Joel Coehoorn
+3  A: 

If its an all-or-nothing fail, then the first format makes sense. If you want to be able to process/return all the non-failing elements, you need to use the second form. Those would be my basic criteria for choosing between the methods. Personally, if it is all-or-nothing, I wouldn't use the second form.

Joe Skora
A: 

setting up a special stack frame for the try/catch adds additional overhead, but the JVM may be able to detect the fact that you're returning and optimize this away.

depending on the number of iterations, performance difference will likely be negligible.

However i agree with the others that having it outside the loop make the loop body look cleaner.

If there's a chance that you'll ever want to continue on with the processing rather than exit if there an invalid number, then you would want the code to be inside the loop.

Matt
+37  A: 

PERFORMANCE:

There is absolutely no performance difference in where the try/catch structures are placed. Internally, they are implemented as a code-range table in a structure that is created when the method is called. While the method is executing, the try/catch structures are completely out of the picture unless a throw occurs, then the location of the error is compared against the table.

Here's a reference: http://www.javaworld.com/javaworld/jw-01-1997/jw-01-hood.html

The table is described about half-way down.

Jeffrey L Whitledge
Okay, so you're saying there's no difference except aesthetics. Can you link to a source?
Michael Myers
Thanks. I suppose things might have changed since 1997, but they'd hardly make it *less* efficient.
Michael Myers
Absolutely is a tough word. In some cases it can inhibit compiler optimisations. Not a direct cost, but it may be an indirect performance penalty.
Tom Hawtin - tackline
True, I guess I should have reigned in my enthusiasm a little-bit, but the misinformation was getting on my nerves! In the case of optimizations, since we can't know which way the performance would be affected, I guess were back to try-it-and-test (as always).
Jeffrey L Whitledge
there is no performance difference, only if the code runs without exceptions.
Onur Bıyık
A: 

The whole point of exceptions is to encourage the first style: letting the error handling be consolidated and handled once, not immediately at every possible error site.

wnoise
Wrong! The point of exceptions is to determinate what exceptional behaviour to adopt when an "error" occurs. Thus choosing inner or outer try/catch block really depends on how you want to handle the loop treatment.
gizmo
That can be done equally well with pre-exception error treatments, such as passing "error occurred" flags around.
wnoise
+13  A: 

All right, after Jeffrey L Whitledge said that there was no performance difference (as of 1997), I went and tested it. I ran this small benchmark:

public class Main {

    private static final int NUM_TESTS = 100;
    private static int ITERATIONS = 1000000;
    // time counters
    private static long inTime = 0L;
    private static long aroundTime = 0L;

    public static void main(String[] args) {
        for (int i = 0; i < NUM_TESTS; i++) {
            test();
            ITERATIONS += 1; // so the tests don't always return the same number
        }
        System.out.println("Inside loop: " + (inTime/1000000.0) + " ms.");
        System.out.println("Around loop: " + (aroundTime/1000000.0) + " ms.");
    }
    public static void test() {
        aroundTime += testAround();
        inTime += testIn();
    }
    public static long testIn() {
        long start = System.nanoTime();
        Integer i = tryInLoop();
        long ret = System.nanoTime() - start;
        System.out.println(i); // don't optimize it away
        return ret;
    }
    public static long testAround() {
        long start = System.nanoTime();
        Integer i = tryAroundLoop();
        long ret = System.nanoTime() - start;
        System.out.println(i); // don't optimize it away
        return ret;
    }
    public static Integer tryInLoop() {
        int count = 0;
        for (int i = 0; i < ITERATIONS; i++) {
            try {
                count = Integer.parseInt(Integer.toString(count)) + 1;
            } catch (NumberFormatException ex) {
                return null;
            }
        }
        return count;
    }
    public static Integer tryAroundLoop() {
        int count = 0;
        try {
            for (int i = 0; i < ITERATIONS; i++) {
                count = Integer.parseInt(Integer.toString(count)) + 1;
            }
            return count;
        } catch (NumberFormatException ex) {
            return null;
        }
    }
}

I checked the resulting bytecode using javap to make sure that nothing got inlined.

The results showed that, assuming insignificant JIT optimizations, Jeffrey is correct; there is absolutely no performance difference on Java 6, Sun client VM (I did not have access to other versions). The total time difference is on the order of a few milliseconds over the entire test.

Therefore, the only consideration is what looks cleanest. I find that the second way is ugly, so I will stick to either the first way or Ray Hayes's way.

Michael Myers
+5  A: 

I agree with all the performance and readability posts. However, there are cases where it really does matter. A couple other people mentioned this, but it might be easier to see with examples.

Consider this slightly modified example:

public static void main(String[] args) {
    String[] myNumberStrings = new String[] {"1.2345", "asdf", "2.3456"};
    ArrayList asNumbers = parseAll(myNumberStrings);
}

public static ArrayList parseAll(String[] numberStrings){
    ArrayList myFloats = new ArrayList();

    for(int i = 0; i < numberStrings.length; i++){
        myFloats.add(new Float(numberStrings[i]));
    }
    return myFloats;
}

If you want the parseAll() method to return null if there is any errors (like the original example), you'd put the try/catch on the outside like this:

public static ArrayList parseAll1(String[] numberStrings){
    ArrayList myFloats = new ArrayList();
    try{
        for(int i = 0; i < numberStrings.length; i++){
            myFloats.add(new Float(numberStrings[i]));
        }
    } catch (NumberFormatException nfe){
        //fail on any error
        return null;
    }
    return myFloats;
}

In reality, you should probably return an error here instead of null, and generally I don't like having multiple returns, but you get the idea.

On the other hand, if you want it to just ignore the problems, and parse whatever Strings it can, you'd put the try/catch on the inside of the loop like this:

public static ArrayList parseAll2(String[] numberStrings){
    ArrayList myFloats = new ArrayList();

    for(int i = 0; i < numberStrings.length; i++){
        try{
            myFloats.add(new Float(numberStrings[i]));
        } catch (NumberFormatException nfe){
            //don't add just this one
        }
    }

    return myFloats;
}
Matt N
Yes, good example.
Michael Myers
That's why I said "If you simply want to catch a bad value but carry on processing, put it inside."
Ray Hayes
A: 

put it inside. You can keep processing (if you want) or you can throw a helpful exception that tells the client the value of myString and the index of the array containing the bad value. I think NumberFormatException will already tell you the bad value but the principle is to place all the helpful data in the exceptions that you throw. Think about what would be interesting to you in the debugger at this point in the program.

Consider:

try {
   // parse
} catch (NumberFormatException nfe){
   throw new RuntimeException("Could not parse as a Float: [" + myString + 
                              "] found at index: " + i, nfe);
}

In the time of need you will really appreciate an exception like this with as much information in it as possible.

Kyle Dyer
Yes, this is a valid point also. In my case I'm reading from a file, so all I really need is the string that failed to parse. So I did System.out.println(ex) instead of throwing another exception (I want to parse as much of the file as is legal).
Michael Myers
A: 

I's like to add my own 0.02c about two competing considerations when looking at the general problem of where to position exception handling:

  1. The "wider" the responsibility of the try-catch block (i.e. outside the loop in your case) means that when changing the code at some later point, you may mistakenly add a line which is handled by your existing catch block; possibly unintentionally. In your case, this is less likely because you are explicitly catching a NumberFormatException

  2. The "narrower" the responsibility of the try-catch block, the more difficult refactoring becomes. Particularly when (as in your case) you are executing a "non-local" instruction from within the catch block (the return null statement).

oxbow_lakes
A: 

That depends on the failure handling. If you just want to skip the error elements, try inside:

for(int i = 0; i < max; i++) {
    String myString = ...;
    try {
        float myNum = Float.parseFloat(myString);
        myFloats[i] = myNum;
    } catch (NumberFormatException ex) {
        --i;
    }
}

In any other case i would prefer the try outside. The code is more readable, it is more clean. Maybe it would be better to throw an IllegalArgumentException in the error case instead if returning null.

Arne Burmeister
A: 

I'll put my $0.02 in. Sometimes you wind up needing to add a "finally" later on in your code (because who ever writes their code perfectly the first time?). In those cases, suddenly it makes more sense to have the try/catch outside the loop. For example:

try {
    for(int i = 0; i < max; i++) {
        String myString = ...;
        float myNum = Float.parseFloat(myString);
        dbConnection.update("MY_FLOATS","INDEX",i,"VALUE",myNum);
    }
} catch (NumberFormatException ex) {
    return null;
} finally {
    dbConnection.release();  // Always release DB connection, even if transaction fails.
}

Because if you get an error, or not, you only want to release your database connection (or pick your favorite type of other resource...) once.

Ogre Psalm33
+2  A: 

As already mentioned, the performance is the same. However, user experience isn't necessarily identical. In the first case, you'll fail fast (i.e. after the first error), however if you put the try/catch block inside the loop, you can capture all the errors that would be created for a given call to the method. When parsing an array of values from strings where you expect some formatting errors, there are definitely cases where you'd like to be able to present all the errors to the user so that they don't need to try and fix them one by one.

This isn't true for this particular case (I'm returning in the catch block), but it's a good thing to keep in mind in general.
Michael Myers
A: 

Another aspect not mentioned in the above is the fact that every try-catch has some impact on the stack, which can have implications for recursive methods.

If method "outer()" calls method "inner()" (which may call itself recursively), try to locate the try-catch in method "outer()" if possible. A simple "stack crash" example we use in a performance class fails at about 6,400 frames when the try-catch is in the inner method, and at about 11,600 when it is in the outer method.

In the real world, this can be an issue if you're using the Composite pattern and have large, complex nested structures.

Jeff Hill