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)});
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With