Throw java exception throw

The next method I created returns a vector (LVector, because the vector is already something), and it throws an exception. Since the method does not matter, do I always return LVector, or when an 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)}); } 
+5
source share
2 answers

When you throw an exception, the method returns nothing (unless this method also catches this exception and has a return statement in the catch clause).

In your method, the return statement will only be executed if no exception is thrown. You must return the LVector in any execution path that does not throw an exception.

+1
source

Throwing an exception completes the method. In this case, nothing will be returned, and the execution of your program will continue at the moment you catch the exception.

0
source

All Articles