Article View: borland.public.jbuilder.java.language
Article #17271Re: quiz
From: Nikolaos Giannopoulos
Date: Mon, 23 Nov 1998 00:00
Date: Mon, 23 Nov 1998 00:00
54 lines
1782 bytes
1782 bytes
-10
Do I get a prize?
The finally statement is ALWAYS executed even if there isn't an
exception. As a matter of fact you can have a try-finally block WITHOUT
a catch block - and it is quite useful. The idea for finally is to do
any housekeeping exception or no exception.
HTH.
--Nikolaos
Edoardo Comar wrote:
> Without minding about bad programming style,
> will this program output 1010 or -10 on (*) ?
>
> package edo.syntaxTest;
>
> public class FinallyTest
> {
>
> public static void main(String[] args)
> {
> FinallyTest finallyTest = new FinallyTest();
>
> System.out.println(finallyTest.test(0));
> System.out.println(finallyTest.test(10)); // (*)
> }
>
> public int test(int param)
> {
> int _result = 1000;
> try
> {
> if(param <= 0)
> throw new IllegalArgumentException("param invalid");
>
> return _result += param;
> }
> catch(Exception exc)
> {
> System.out.println("catch!");
> _result = -20;
> }
> finally
> {
> System.out.println("finally!");
> _result = -10;
> }
> return _result;
> }
> }
Technical Details
Message-ID:
<3659C0FF.7B05F52C@solmar.ca>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<73b97j$5q012@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,f51b0f21d8f8b176 X-Google-Attributes: gidf74b3,public From: Nikolaos Giannopoulos <nikolaos@solmar.ca> Subject: Re: quiz Date: 1998/11/23 Message-ID: <3659C0FF.7B05F52C@solmar.ca> X-Deja-AN: 414904851 Content-Transfer-Encoding: 7bit References: <73b97j$5q012@forums.borland.com> To: Edoardo Comar <comar@tecnest.it> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii Organization: Solmar Solutions Inc. Mime-Version: 1.0 Reply-To: nikolaos@solmar.ca Newsgroups: borland.public.jbuilder.java.language