Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)});
}
like image 654
Sean Letendre Avatar asked Nov 08 '22 15:11

Sean Letendre


1 Answers

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.

like image 111
Eran Avatar answered Nov 14 '22 21:11

Eran