Which is more efficient
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Which is more efficient

  1. #1
    Join Date
    Feb 2011
    Posts
    15

    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 { }

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    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.

  3. #3
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    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)

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center