-
April 14th, 2011, 08:50 AM
#1
Which is more efficient
I am reading some settings from a config file that would be setup by different developers. They can choose to add different settings and leave out other ones. In by abstract classes constructor, I want to read those settings, but I don't want to fail if they don't exist. So my question is, which is more efficient: using a try with an empty catch block, or using a try with an empty finally block? Again, I don't want to do anything if there's an exception.
Example:
Code:
try
{
writeDebugLog = ConfigSectionHandler.ReadSetting(AppSection, SettingKeyWriteDebugLog, false);
}
catch { }
Or
Code:
try
{
writeDebugLog = ConfigSectionHandler.ReadSetting(AppSection, SettingKeyWriteDebugLog, false);
}
finally { }
-
April 14th, 2011, 09:39 AM
#2
Re: Which is more efficient
If you're worried about efficiency you're worried about the wrong thing. One of those will catch and consume the exception the other will let the exception propagate. Empty finally blocks serve no purpose at all
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
April 14th, 2011, 09:51 AM
#3
Re: Which is more efficient
If you want to do something with an exception - even ignore it, you'll need a catch regardless of performance. (which basically is only a factor if an exception occurs)
-
April 15th, 2011, 02:54 AM
#4
Re: Which is more efficient
The ConfigSectionHandler is your own class (I cannot find it anywhere)? Implenet a test method like SettingExists to test if the desired setting is there or not. Try-catch is intended to deal with unexpected conditions, but absence of the setting is expected, it is one of regular states, in which the setting can be, nothing expectional.
P.S. If I could advise you, never do empty catch block, at least log the exception, otherwise the exception is consumed and you could be in trouble while investigating why something goes wrong
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|