1

In Visual Studio, when I do a Surround with ... try, I get this:

try
{
}
catch (Exception)
{
    throw;
}        

I want this:

try
{
}
catch (Exception exception)
{
    throw;
}        

Is there a way to change the default?

Edit:
Microsoft Visual Studio Enterprise 2015
Version 14.0.25425.01 Update 3

5
  • Which version of VS some have snippet manager Aug 8, 2017 at 15:05
  • @ISun I was thinking of that, but it's not a snippet, it's an intellisense command Aug 8, 2017 at 15:06
  • It looks like "Surround with" commands are code snippets too. msdn.microsoft.com/en-us/library/6hf704tz.aspx Aug 8, 2017 at 15:09
  • yes they are snippets (at least in vs2017), I checked Snippet-Manager to find the try.snippet file, changed it and used it as you described... it works...
    – René Vogt
    Aug 8, 2017 at 15:11
  • So I go to Tools > Code Snippet Manager. then Select Language: CSharp, then expand Visual C#, scroll down and select "try" and the Location text box shows: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C#\try.snippet. Now what? Aug 8, 2017 at 16:23

1 Answer 1

6

You can create your own snippet to replace the default one for try.

Snippets are generally stored as actual files inside a folder created by your Visual Studio installation. See this CodeProject article for the locations of this directory in different versions of Visual Studio.

In Visual Studio 2015, you go to Tools → Code Snippets Manager to edit your snippets.

As asked in the comments about next steps, go to the folder location (C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C#) through windows Run command in windows explorer,open the try.snippet in administrator mode any editor like visual studio code or notepad. Find the part shown below.

<Code Language="csharp"><![CDATA[try 
    {           
        $selected$
    }
    catch ($expression$)
    {
        $end$
        throw;
    }]]>

In the part shown above, find catch($expression$) and change it to catch($exception$ exception), and save the file. You are done now and when you will insert snippet into visual studio now, it will add the variable.

5
  • I've been trying this and can't get it to work. I have a file in C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C#, called tryex.snippet which I have edited as you describe. (This took some dancing around file permissions, but it's there now). So now there are 3 try snippets in the directory. try.snippet, tryf.snippet and the one I have created. If I go to the "Surround with..." menu selection, there is a new entry there, but it is not 'tryex', merely 'try'. Nonetheless I select it and it doesn't have 'exception' in it. Aug 15, 2017 at 14:16
  • 1
    There is a button in Tools → Code Snippets Manager which says import. Create your snippet somewhere on the machine and import through the button. It will be visible. Before this import, open the file into notepad like editor and change title and shortcut tags in xml structure. Aug 16, 2017 at 2:20
  • Or you can click on Add button, find a folder on your machine which will contain your snippets. And click on OK. Thereafter, you can just create your snippet inside that folder with unique title and shortcut and it will work. Aug 16, 2017 at 2:34
  • I had tried importing before and was getting this error: The code snippet cannot be saved to this location. The location was "My Code Snippets". I just tried importing to "Visual C#" and this did the import. Then going to "Surround with..." , the entry for it is still just "try" not "tryex", but I use it and the "exception" is there, so close enough. Aug 16, 2017 at 14:07
  • Oh, I see now that the name of the snippet with Visual Studio is set in the xml file in the <Title> tag Aug 16, 2017 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.