Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Where to put the return Date: Fri, 11 Aug 2017 12:24:00 +0200 Organization: Decebal Computing Lines: 42 Message-ID: <87shgyz9kv.fsf@munus.decebal.nl> References: <87zib6zeup.fsf@munus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="b5572a6689d7cc39c1ab3122a4c52947"; logging-data="7985"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18SOXudWCqV4ErRS7lglEt56/Iw56R+8uk=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:mLEtf9AjJyTiZNX3bVcZMfXVJOg= sha1:TCLXbS088MaWhQI/hDjkZvg25hQ= Xref: csiph.com comp.lang.java.programmer:37134 ram@zedat.fu-berlin.de (Stefan Ram) writes: > Cecil Westerhof writes: >>Often I see the return in the try block. But personally I prefer it >>this way. Is there a preferred way, or are both ways acceptable? > > It might just be a question of being compilable. > > For example, in the following case, I get a compile-time error: > > Main.java > > public final class Main > { > public static int f() > { try( final java.util.Scanner scanner = new java.util.Scanner( "" )) > { return 1; } > catch( final java.lang.RuntimeException exception ) > {} } > > public static void main( final java.lang.String[] args ) > { java.lang.System.out.println( f() ); }} > > transcript > > Main.java: error: missing return statement > {} } > ^ > 1 error > > The compile-time error disappears once the catch clause > is removed or the return is moved to the end of the method. Yes, the examples I saw did not use a catch clause. Well, if there is no reason not to move the return out of the try block, I will keep doing that. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof