0

What is a better approach when it comes to writing code for sharepoint 2010 applications:

 using (SPSite currentSite = new SPSite("SiteName"))
 {
     using (SPWeb currentWeb = currentSite.OpenWeb())
     {
        try{...}
        catch{...}
     }
 }

or

try 
{
  using (SPSite currentSite = new SPSite("SiteName"))
  {
     using (SPWeb currentWeb = currentSite.OpenWeb())
     {
        ....
     }
  }
}
catch{...}

? Thank you!

Hank Mooody
  • 527
  • 11
  • 29
  • 1
    They both do different things, which is "better" depends on which behavior you want for your application. Neither is better without context. – Servy Jan 08 '16 at 14:12

1 Answers1

0

from a similar thread( Does Dispose still get called when exception is thrown inside of a using statement? )

the top rated answer there suggests that both examples you gave will be alright considering resources disposal:

using wraps your code in a try/finally block where the finally portion will call Dispose() if it exists.

so the remaining concern might be the resource acquisition, which might throw exceptions as well(been working with sharepoint for a couple of years now and seemingly everything in this framework is throwing exceptions at you sooner or later). so i'd suggest using your second example to catch those.

Community
  • 1
  • 1
garglblarg
  • 576
  • 1
  • 8
  • 16