aspose file tools*
The moose likes Java in General and the fly likes Is it good to use finally block only for logging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is it good to use finally block only for logging "method end"" Watch "Is it good to use finally block only for logging "method end"" New topic
Author

Is it good to use finally block only for logging "method end"

Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi,

I have seen many codes in which finally block is only used to just put one logger of "ending method", is it the good use of it?


Regards, Abdul Mohsin
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
I'm assuming the whole method is wrapped by a try clause.

It's really an issue of style, but I think it's personally reasonable.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Hm, it seems of limited use. If I'm logging the end of a method, I would probably like to know whether the method completed normally or threw an exception. Putting the log statement in a finally treats these two cases exactly the same. In general I would rather use-aspect oriented programming here to insert log statements to automatically log any return statement, as a return, and log any error as an error, without cluttering up the code. While we're at it, it's often nice to log the return value, if there is one.


"I'm not back." - Bill Harding, Twister
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi Jim,

Can you please elaborate more how you will use "aspect oriented programming to insert "method start" and "method end" logs".

Do you find it valid to use finally to just log one logger?

All,

Do finally cause an extra overhead as compared to code with no finally block?

Regards,

Abdul Mohsin
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Abdul]: Can you please elaborate more how you will use "aspect oriented programming to insert "method start" and "method end" logs".

There's a good article on the subject here. There are probably other ways to do it using other aspect-oriented frameworks, like Spring AOP and JBoss AOP.

[Abdul]: Do you find it valid to use finally to just log one logger?

I suppose, but I don't think it's very useful, for the reasons I mentioned.

[Abdul]: Do finally cause an extra overhead as compared to code with no finally block?

In general, no, the finally block really doesn't create any special overhead. Of course whatever you do in the finally block might, if it's a slow operation. But if it's something that needs to be done, it needs to be done.
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi Jim,

Thanks a lot for answering my all questions so beautifully.
 
 
subject: Is it good to use finally block only for logging "method end"