Categories
discuss

Return from method, in the “try” block or after “catch” block?

Is there any difference between following two methods?

Which one is preferable and why?

Prg1:

public static boolean test() throws Exception {
    try {
        doSomething();
        return true;
    } catch (Exception e) {
        throw new Exception("No!");
    }    
}

Prg2:

public static boolean test() throws Exception {
    try {
        doSomething();
    } catch (Exception e) {
        throw new Exception("No!");
    }
    return true;    
}

Answer

Consider these cases where you’re not returning a constant expression:

Case 1:

public static Val test() throws Exception {
    try {
        return doSomething();
    } catch (Exception e) {
        throw new Exception("No!");
    }
    // Unreachable code goes here
}

Case 2:

public static Val test() throws Exception {
    Val toReturn = null;
    try {             
        toReturn = doSomething();
    } catch (Exception e) {
        throw new Exception("No!");
    }
    return toReturn;
}

I would prefer the first one. The second is more verbose and might cause some confusion when debugging.

If test() incorrectly returns null, and you see toReturn being initialized to null, you might think the problem is in test() (especially when test() is not just a simple example like this).

Even though it can only return null if doSomething returns null. But that might be hard to see at a glance.


You could then argue that, for consistency’s sake, it’s better to always use the first form.

Categories
discuss

java exception throw return

The following method I have created return a vector (LVector because vector is already something) and it throws an exception. Since the method is nonvoid do I have do always return an LVector, or when the exception is thrown does the method just cancel itself?

public static LVector crossProduct(LVector v1, LVector v2) throws LCalculateException{
    if(v1.getLength() != 3|| v2.getLength() != 3)
    throw new LCalculateException("Invalid vector lengths");

    return new LVector(new double[3]{v1.get(1)*v2.get(2)-v1.get(2)*v2.get(1),v1.get(2)*v2.get(0)-v1.get(0)*v2.get(2),v1.get(0)*v2.get(1)-v1.get(1)*v2.get(0)});
}

Answer

When you throw an exception, the method doesn’t return anything (unless the method also catches that exception and has a return statement in the catch clause).

In your method, the return statement will only be executed if the exception isn’t thrown. You have to return a LVector in any execution path that doesn’t throw an exception.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..