user3023451
user3023451

Reputation:

Auto-generate a try catch block in visual studio 2010

Anyone know if there is a keystroke shortcut or option to autogenerate a try/catch block around a statement in Visual Studio 2010? I can see what exceptions are thrown if I look at the overlay documentation when I hover over a statement. I'd like to right click -> generate try/catch, as it would save a lot of time in handling all possible cases.

Is this possible?

Upvotes: 32

Views: 49251

Answers (6)

Sam
Sam

Reputation: 317

If you don't want to remove your fingers from the keyboard, you can hit Ctrl + K, Ctrl + S, then T, then Enter. That's basically what he said, but without using the mouse.

http://msdn.microsoft.com/en-us/library/6hf704tz%28VS.80%29.aspx

Upvotes: 14

OzBob
OzBob

Reputation: 4520

There is now a visualstudio 2017 extension: https://marketplace.visualstudio.com/items?itemName=ZTransform.TryCatch

If you want to use it with VS2019: download, change extension to zip, unzip, follow these instructions https://devblogs.microsoft.com/visualstudio/how-to-upgrade-extensions-to-support-visual-studio-2019/, zip, rename to .vsix and install.

Upvotes: 0

sshow
sshow

Reputation: 9084

Using the mouse

  1. Mark your code

  2. Right-click

  3. Select Surround with...

  4. Double-click try

Using the keyboard #1

  1. Mark your code using Shift, Ctrl + A, or whatever works for you

  2. Press Menu key / Application key (alternatively Shift + F10)

  3. Type S

  4. Type T

  5. Press Enter or Tab

Using the keyboard #2 (as perlox and Fredrik Norlin points out)

  1. Mark your code using Shift, Ctrl + A, or whatever works for you

  2. Press Ctrl + K, followed by CTRL + S

  3. Type T

  4. Press Enter or Tab

Upvotes: 84

Peter Perháč
Peter Perháč

Reputation: 20782

type try then hit Tab,Tab

2 x Tab activates a code snippet.

type tryf , Tab,Tab to activate the try..finally block instead

Upvotes: 44

Jeff
Jeff

Reputation: 163

I use CodeRush from DevExpress. I can use the key strokes "TC" to generate. It works with C# too.

    Try

                Catch ex As Exception
                    dmPrgm_Err(Err, ex)
                End Try

I then can use TSC for a Try SQL Catch with custom message boxes.

            Try

                Catch ex As SqlException
                    dmSQLErr(ex)
                Catch ex As Exception
                    dmPrgm_Err(Err, ex)
                End Try

Upvotes: 0

Lucas B
Lucas B

Reputation: 12013

Have you tried using snippets?

Upvotes: 4

Related Questions