File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bill Shirley
Ranch Hand
Posts: 457
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 111
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,

Thanks a lot for answering my all questions so beautifully.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic