- In the code editor, select a block of code that you want to surround.
- On the main menu, choose ReSharper | Edit | Surround With Template, or press Ctrl+Alt+J. The Quick access list of available templates appears.
- Select a template from the list. For a complete list of all available templates, click More and select the desired template from the Choose Template dialog box. To optionally introduce a template into the Quick access list, select its entry in the Choose Template dialog box and select Add to quicklist in the lower part of the dialog box before clicking OK.
- If the expanded template requires user input, the editor switches to the template editing mode and suggests an input position. Type the desired value, or accept the default, suggested by ReSharper, and press Tab or Enter to move to the next input position, Shift+Tab to move the input focus to the previous input position, and Esc key to switch the template editing mode off and move the caret to the end position defined by the template developer.
Consider the following code fragment:
We would like to enclose the statement in a
try..catch block to take care of possible
System.IO.IOExceptions thrown. ReSharper conveniently offers a try..catch predefined template to streamline this operation.
Pressing Ctrl+Alt+J opens a drop-down list with available templates:
To choose the try..catch template, you can do one of the following:
- Click it with the mouse.
- Go down to the second line and press Enter.
- Press 8.
We supply the desired exception type (
IOException) at the first input field and press Tab or Enter to move on to the next parameter. At the second input position, we change the name of the exception variable suggested by ReSharper to
ioExc, and press Tab or Enter again to quit the template editing mode. The caret and selection are moved to the positions specified by the template designer. The code in the editor now looks as follows:
Thus, if the basic exception handling code provided by ReSharper does not suit your needs, simply type your own catch block body in place of the suggested code.