• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logging entry & exit for methods that throw exceptions

 
Lara McCarver
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am starting to add logging to my application. My question is, if you have a method that might throw an exception, then under normal circumstances, you wouldn't get an exit statement. If you are throwing the exceptions yourself, you could put the exit logging in your catch() clause before you throw it, but what if you have a method that throws a pass-through exception?

For example, consider a read() method that throws an IOException. The IOException is thrown because the read() method calls a method in RandomAccessFile which throws IOException. So this method wouldn't ordinarily have a catch clause. I can see only 2 choices:

1. Wrap most of the method in a try/finally statement, with the exit logging in the finally clause.

OR

2. Put a try/catch statememnt around RandomAcccesFile.read(). In the catch statement, catch the IOException, log the exit, and then rethrow the exception.

Neither one of these sounds very appealing to me. Choice 1. seems simpler, but I worry about the overhead... does it slow the program down a lot to be doing so many try/catch statements? Or does the try/catch mechanism have little performance impact if the catch is not used?
[ June 12, 2005: Message edited by: Lara McCarver ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anything aboout logging in your requirements? I didn't do any of that, and my grader was quite pleased.
 
Lara McCarver
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The logging is for my own use... testing :roll:
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lara,

Personally, I would not log anything in your method this case - I would leave it to the class and method that finally does do something with the exception to do some logging.

To give a convoluted example, lets imagine that my Data class is just a Fa´┐Żade to my DataFile class. In my DataFile class, my public read(recordNumber) method calls a private readRecord(Offset) method. Neither method in my DataFile class catch IOException.

So if you were looking at the log messages later, you would see that:
  • Data.read was called
  • DataFile.read was called
  • DataFile.readRecord was called
  • Data caught IOException and was handling it.
  • If I was concerned about which line of DataFile.readRecord threw the IOException, then I would log the stack trace in the Data.read method (which can be a good idea at any time).

    I wouldn't worry about logging the exception in DataFile.readRecord, as I have already declared that I am not interested in that exception by listing it in the throws clause.

    Regards, Andrew
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic