Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Do you need finally when your method throws Exception..

Reply
Thread Tools

Do you need finally when your method throws Exception..

 
 
Tomas
Guest
Posts: n/a
 
      09-28-2005

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


 
Reply With Quote
 
 
 
 
Stefan Schulz
Guest
Posts: n/a
 
      09-28-2005
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"


 
Reply With Quote
 
 
 
 
Jon Haugsand
Guest
Posts: n/a
 
      09-28-2005
* http://www.velocityreviews.com/forums/(E-Mail Removed)
> 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, (E-Mail Removed)
http://www.ifi.uio.no/~jonhaug/, Phone: +47 22 85 24 92
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      09-28-2005
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/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-28-2005
On 28 Sep 2005 22:01:26 GMT, "Tomas" <(E-Mail Removed)> 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.
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      09-29-2005
"Tomas" <(E-Mail Removed)> wrote in
news:433b12b6$0$16194$(E-Mail Removed):

>
> 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.
 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      09-29-2005
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/
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      09-29-2005
Thomas Hawtin <(E-Mail Removed)> wrote in news:433c3fd0$0$73618
$(E-Mail Removed):

> 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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-29-2005
On Thu, 29 Sep 2005 18:29:55 GMT, zero <(E-Mail Removed)> 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.
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      09-29-2005
Roedy Green <(E-Mail Removed) > wrote in
news:(E-Mail Removed):

> On Thu, 29 Sep 2005 18:29:55 GMT, zero <(E-Mail Removed)> wrote or quoted
>:
>
>> if(resource == null)
>> resource.close();

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


oops, of course. Thanks for the correction
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[coding practises] do you declare your superclass "throws"?? jrobinss Java 32 01-28-2008 04:17 PM
Try...Catch...Finally not firing finally? David Lozzi ASP .Net 12 05-11-2007 12:41 AM
calling a method with 'throws IOException' from another method in the same class rleroux@telus.net Java 1 01-28-2007 06:30 AM
Developer poll. return within finally: I need your opinons Duncan Strang Java 6 03-03-2004 08:45 PM
Directory.CreateDirectory Method throws UnauthorizedAccessException GM ASP .Net 2 01-16-2004 10:50 PM



Advertisments