The compiler is your friend... just keep it happy. While this little situation seems like an anti-pattern, it's not hurting anything, so I just code in this style and don't worry about it.
Is this just an error with Eclipse, or does javac give the same error? I think this may just be an artifact of over-zealous code analysis. Sunburned Surveyor wrote: > I've got a quick question about the best way to handle return values > in Java methods with a try/catch statement. Consider, as an example, > the following method: > > public static SurveyorsAngle getSupplementaryAngle(SurveyorsAngle argAngle) > { > try > { > SurveyorsAngle halfRevolution = new BasicSurveyorsAngle(0.50); > SurveyorsAngle toReturn = > AngleCruncher.subtractAngles(halfRevolution, argAngle); > return toReturn; > } > > catch(ShouldNeverReachHere caught) > { > System.err.println(caught.getMessage()); > } > > return null; > } > > In this example the return value must be set within the try statement, > becuase the subtractAngles method throws an exception. > > This means that the method has to return a null value after the > try/catch statements. However, this return statement will never be > reached, because the method will either [1] return from the try > statement with a valid SurveyorsAngle object or [2] execution will > shift to the catch statement. > > However, if I remove the return null statement from the end of this > method, Eclipse gives me a compile error. This error states that the > getSupplementaryAngle method must return a SurveyorsAngle value. > > I seem to run into this a lot in my code. I know I could just > eiliminate the try/catch statement and add a throws clause to the > containing method, but sometimes the exception SHOULD be handled in > the containing method. > > Is there a better solution? It seems like the compiler is requiring a > return statement that can never be reached. > > Thanks for any advice on this. > > The Sunburned Surveyor > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Jump-pilot-devel mailing list > Jump-pilot-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel > > -- Martin Davis Senior Technical Architect Refractions Research, Inc. (250) 383-3022 ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Jump-pilot-devel mailing list Jump-pilot-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel