Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everybody,

I need some support in best practices.

I work on a service, running each minute on a server. Suppose I open a file to register some actions, a log file. I get an exception, and I need to someway close the file, without inserting all the code in a Try Catch block...
Next time the timer executes, it tries to access again the file and it is opened, so can't add information.

What would you do?

Please help me.

Thanks in Advance.
Posted
Updated 11-Jul-11 23:40pm
v2
Comments
johannesnestler 12-Jul-11 5:45am    
??? insert your code in try catch finally block. Whats your problem with that (+ there is no other solution)
garfield185 12-Jul-11 5:56am    
I agree, my problem is that huge try catch blocks (in this case, hundreds of lines) are not best practices
johannesnestler 12-Jul-11 6:26am    
So... Split your Code in smaller functions and wrap them in TRY catch. If this is too difficult you could use regions to improve readability.

You use the finally block to close the file.

try
{
// open
}
catch
{
// oops
}
finally
{
// close, this is always called.
}
 
Share this answer
 
Comments
garfield185 12-Jul-11 5:48am    
Actually, the problem is more complex. Suppose I open a file, inside a function, which throws the exception. The catch is OUTSIDE the funcion... So I can't close that file that I opened inside the function... I don't know how to do it without inserting all that code inside the function in a try catch.
Christian Graus 12-Jul-11 5:50am    
Well, you can do a try/catch/finally inside the function to make sure it is closed, and then rethrow your exception if you want.
garfield185 12-Jul-11 5:54am    
Yeah, I agree, It's just that I'm not sure of making a try catch block of hundreds of lines, just to make sure that I close the file. Isn't other way? Best practices suggest not to do huge try catch blocks, it is supposed to use it on exact lines that can throw an exception.
BobJanova 12-Jul-11 5:59am    
The file handling section should not be hundreds of lines. That is your best practice violation, imo. It should be of the order of

public void Log(string message){
lock(fileIOSyncObject){
using(StreamWriter sw = new StreamWriter(LOG_FILE, FileMode.Append)){
sw.WriteLine(message);
}
}
}

(pseudo-code, I can't remember the actual arguments to StreamWriter to not truncate the file off the top of my head. The lock is to prevent concurrent log attempts as Reiss mentions.)
garfield185 12-Jul-11 6:05am    
What I'm doing right now, is:
- open the log file,
- do whatever (hundreds of lines here, that may or not write on the log many times),
- close the file.

Do you think that I should open and close the log file each time I need to write instead of doing it once, as I do?

What is recommended?
When accessing a database, would it be the same? Open and close connections instead of doing it only once?
I think what you are after is

timer_fired_event method 
{ 
try 
{ 
open_log_file 
call_method_with_other_logic()
} 
catch 
// these should be fatal exceptions throw either by the file open, 
//or in the called method
{ 
} 
finally
{ 
close_log_file 
} 

private void call_method_with_other_logic()
{ 
// this may or may not write to open log file 
// this may or may not throw an exception 
// you may want to catch some exceptions here and handle them if appropriate or // let them bubble up the call stack 
}
 
Share this answer
 
Comments
garfield185 12-Jul-11 6:27am    
Great. thanks a lot.
Given that you are using a timer and therefore working in a multi-threaded environemnt, it is very likely that you are going to get IO exceptions thrown, so you are going to have to catch them and handle them - without knowing the scope of your application, is just closing the file always the right thing to do?

You may want to add some sync locking in your timer fired code to also prevent multiple attempts to open the file.
 
Share this answer
 
Comments
garfield185 12-Jul-11 5:52am    
Every time the timer elapses, I stop it until the process finishes, so there is no chance of multiple attempts to open it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900