Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Do you need finally when your method throws Exception.. (http://www.velocityreviews.com/forums/t146535-do-you-need-finally-when-your-method-throws-exception.html)

Tomas 09-28-2005 10:01 PM

Do you need finally when your method throws Exception..
 

Hi,

If you have a method that throws some Exception lik this:

public void doSomething(){throws SomeException

Do you then need a finally block in that method, if you i.e want to close
streams and such. I mean when this Exception occurs do it jump out of the
method direct or does it finish the rest of the method.

I wonder cause i tried to have a finally block in a method that did not have
any try and catch blocks så the compiler complained.

Best Regards
/Tomas



Stefan Schulz 09-28-2005 10:14 PM

Re: Do you need finally when your method throws Exception..
 
On Wed, 28 Sep 2005 22:01:26 +0000, Tomas wrote:

> Do you then need a finally block in that method, if you i.e want to close
> streams and such. I mean when this Exception occurs do it jump out of the
> method direct or does it finish the rest of the method.


It abruptly terminates the method. This means that nothing after the
exception is executed, and the method immediately returns with an
Exception - unless you put some code into a finally block

--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"



Jon Haugsand 09-28-2005 10:19 PM

Re: Do you need finally when your method throws Exception..
 
* t_granat@hotmail.com
> Hi,
>
> If you have a method that throws some Exception lik this:
>
> public void doSomething(){throws SomeException
>
> Do you then need a finally block in that method, if you i.e want to close
> streams and such. I mean when this Exception occurs do it jump out of the
> method direct or does it finish the rest of the method.
>
> I wonder cause i tried to have a finally block in a method that did not have
> any try and catch blocks så the compiler complained.


You should catch the exception in question if you can use a finally
block. But don't try to catch too many exceptions and only those you
can cope with. Bad exceptions, i.e. things that has to do with
programming errors, should never be catched anywhere (but at some
point where logging and bug reporting can take place).

--
Jon Haugsand
Dept. of Informatics, Univ. of Oslo, Norway, mailto:jonhaug@ifi.uio.no
http://www.ifi.uio.no/~jonhaug/, Phone: +47 22 85 24 92

Thomas Hawtin 09-28-2005 10:22 PM

Re: Do you need finally when your method throws Exception..
 
Tomas wrote:
>
> Do you then need a finally block in that method, if you i.e want to close
> streams and such. I mean when this Exception occurs do it jump out of the
> method direct or does it finish the rest of the method.


Whenever you acquire a resource, you must make sure always release it.
If your method allocates a resource and then throws an exception
removing any reference to the resource, you should ensure that the
resource is released.

> I wonder cause i tried to have a finally block in a method that did not have
> any try and catch blocks så the compiler complained.


You need the try block. The syntax should be:

try {
...
} finally {
...
}

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green 09-28-2005 11:30 PM

Re: Do you need finally when your method throws Exception..
 
On 28 Sep 2005 22:01:26 GMT, "Tomas" <t_granat@hotmail.com> wrote or
quoted :

>Do you then need a finally block in that method, if you i.e want to close
>streams and such. I mean when this Exception occurs do it jump out of the
>method direct or does it finish the rest of the method.


an exception leaves the method immediately (if there is no try
block).. The idea is something has gone wrong so calculation cannot
proceed. E.g. NullPointerExeception. There is nothing we CAN do
without even an object to run the next method on.

It will keep propagating up blocks and calls till it finds a try block
willing to catch the exception. In each case it exits the block or
method immediately.

Of course you can use finally to do your cleanup, most commonly file
closing.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

zero 09-29-2005 06:29 PM

Re: Do you need finally when your method throws Exception..
 
"Tomas" <t_granat@hotmail.com> wrote in
news:433b12b6$0$16194$ba620e4c@news.skynet.be:

>
> Hi,
>
> If you have a method that throws some Exception lik this:
>
> public void doSomething(){throws SomeException
>
> Do you then need a finally block in that method, if you i.e want to
> close streams and such. I mean when this Exception occurs do it jump
> out of the method direct or does it finish the rest of the method.
>
> I wonder cause i tried to have a finally block in a method that did
> not have any try and catch blocks så the compiler complained.
>
> Best Regards
> /Tomas
>
>


The best way to understand the program flow when using exceptions is just
to try it out. Set output statements everywhere you can, and then
examine what you get. Something like this:

public static void method1() throws Exception
{
System.out.println("method1 starts");
throw new Exception("exception in method1");
System.out.println("method1 ends");
}

public static void main(String args[])
{
System.out.println("main starts");
try
{
method1();
}
catch(Exception e)
{
System.out.println("exception caught");
e.printStackTrace();
}
finally
{
System.out.println("finally block executes");
}
System.out.println("main ends");
}

Here you will see that the "method1 ends" output will never be reached,
because the program flow is interrupted by the Exception. If you comment
out (or remove) the throw line, method1 will end normally, and the
"exception caught" line will be skipped. In either case the finally
block will be executed though.
You can never have a catch or finally block without a try block.

If you open resources locally in method1, it is best to do something like
this:

public void method1() throws Exception
{
SomeResource resource = null;
try
{
// open and use the resource
}
finally
{
if(resource == null)
resource.close();
}
}

Here the resource will be closed, wether or not an exception occurs,
because the finally block always executes. Since you're not catching any
exceptions, they will be thrown to be handled by the calling method.

Of course ideally you should not use Exception, but a subclass of it.

Thomas Hawtin 09-29-2005 07:25 PM

Re: Do you need finally when your method throws Exception..
 
zero wrote:
>
> SomeResource resource = null;
> try
> {
> // open and use the resource
> }
> finally
> {
> if(resource == null)
> resource.close();
> }
> }


It's much better to write:

final SomeResource resource = ...acquire resource ... ;
try {
... use the resource ..
} finally {
resource.close();
}

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/

zero 09-29-2005 08:43 PM

Re: Do you need finally when your method throws Exception..
 
Thomas Hawtin <usenet@tackline.plus.com> wrote in news:433c3fd0$0$73618
$ed2619ec@ptn-nntp-reader03.plus.net:

> zero wrote:
> >
>> SomeResource resource = null;
>> try
>> {
>> // open and use the resource
>> }
>> finally
>> {
>> if(resource == null)
>> resource.close();
>> }
>> }

>
> It's much better to write:
>
> final SomeResource resource = ...acquire resource ... ;
> try {
> ... use the resource ..
> } finally {
> resource.close();
> }
>
> Tom Hawtin



It's quite possible that the method to acquire the resource returns null.
To account for this possibility without ignoring other possibilities, I
always assign it to be null first, and in the finally block check for that.
NullPointerExceptions are no fun.

Roedy Green 09-29-2005 08:45 PM

Re: Do you need finally when your method throws Exception..
 
On Thu, 29 Sep 2005 18:29:55 GMT, zero <zero@this.hi> wrote or quoted
:

> if(resource == null)
> resource.close();

you meant to say

if ( resource != null )
{
resource.close();
}
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

zero 09-29-2005 08:50 PM

Re: Do you need finally when your method throws Exception..
 
Roedy Green <my_email_is_posted_on_my_website@munged.invalid > wrote in
news:sikoj11ghakm7kufces5c369u8iip78po8@4ax.com:

> On Thu, 29 Sep 2005 18:29:55 GMT, zero <zero@this.hi> wrote or quoted
>:
>
>> if(resource == null)
>> resource.close();

> you meant to say
>
> if ( resource != null )
> {
> resource.close();
> }


oops, of course. Thanks for the correction :-)


All times are GMT. The time now is 05:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.