Auto-generate a try catch block in visual studio 2010
Solution 1
Using the mouse
Mark your code
Right-click
Select
Surround with...
Double-click
try
Using the keyboard #1
Mark your code using Shift, Ctrl + A, or whatever works for you
Press
Menu key
/Application key
(alternatively Shift + F10)Type S
Type T
Press Enter or Tab
Using the keyboard #2 (as perlox and Fredrik Norlin points out)
Mark your code using Shift, Ctrl + A, or whatever works for you
Press Ctrl + K, followed by CTRL + S
Type T
Press Enter or Tab
Solution 2
type try
then hit Tab,Tab
2 x Tab activates a code snippet.
type tryf
, Tab,Tab to activate the try..finally
block instead
Solution 3
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
Solution 4
Have you tried using snippets?
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on January 22, 2021Comments
-
Admin over 3 years
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?