# コード箇所を囲む

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/ja-jp/help/idea/llms.txt).

IntelliJ IDEA は、ソースコードの言語に基づいていろいろな構造を持つ周囲のコードフラグメントの標準テンプレートを提供します。 これには、  `if...else`  条件文、  `do...while`  と  `for`  ループ、  `try...catch...finally`  の組み合わせ、XML タグ、折りたたみ領域、その他の構造が含まれます。

Procedure: 言語構造でコードブロックを囲む

1. コードフラグメントを選択します。

2. `Ctrl+Alt+T` (Windows), `⌘ ⌥ T` (macOS), `⌘ ⌥ T` (IntelliJ IDEA Classic (macOS)), `⌘ ⌥ T` (macOS System Shortcuts), `Ctrl+Alt+Shift+B` (XWin), `Ctrl+Alt+Shift+B` (GNOME), `Ctrl+Alt+Shift+B` (KDE), `Ctrl+Alt+T` (Emacs), `Ctrl+Alt+T` (Sublime Text), `⌘ ⌥ T` (Sublime Text (macOS)), `Ctrl+Alt+T` (Visual Studio), `Ctrl+Alt+T` (Visual Studio (macOS)), `Ctrl+Alt+T` (NetBeans), `Alt+Shift+Z` (Eclipse), `⌘ ⌥ Z` (Eclipse (macOS)) を押すか、メインメニューで  `コード | 囲む`  に移動します。

または、表示される [ツールバー](working-with-source-code.html#floating_toolbar)で   囲む   をクリックします。

3. リストから必要なサラウンドステートメントを選択します。 オプションの上にマウスを移動すると、そのステートメントを選択した場合に適用される変更のプレビューが表示されます。

![オプション付きサラウンドのプレビュー](https://resources.jetbrains.com/help/img/idea/2026.3/surround_with_preview.png)

囲みステートメントで使用されるコードテンプレートを編集できます。   設定   ダイアログ ( `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS)) ) で、  `エディター | ファイルおよびコードテンプレート`  に進みます。

例:  `try...catch`  条件文の final 修飾子、名前、さらには例外の種類を設定できます。

![コードテンプレート設定](https://resources.jetbrains.com/help/img/idea/2026.3/code_template.png)

> **Note:**
> ライブテンプレートに [$SELECTION$ 変数](template-variables.html#pdtv)が含まれている場合は、サラウンドステートメントとして使用できます。
>
>
>
> 独自のサラウンドステートメントを作成するには、 [ライブテンプレートを作成し](creating-and-editing-live-templates.html)、テンプレートテキストで  `$SELECTION$`  変数を使用します。

## サンプル

次の例では、  `int number = scanner.nextInt();`  Java ステートメントを選択し、それに   try / catch   surround ステートメントを適用します。

Before:

```JAVA
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        System.out.println("You entered: " + number);
    }
}

```

After:

```JAVA
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = 0;
        try {
            number = scanner.nextInt();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("You entered: " + number);
    }
}

```

この機能は、次の言語（コンテキスト）に適用されます:

|  コンテキスト  |  囲む  |
| --- | --- |
|  Java ステートメント  |      * `if`    * `if/else`    * `while`    * `do/while`    * `for`    * `try/catch`    * `try/finally`    * `try/catch/finally`    * `synchronized`    * `Runnable`    * `{}`    |
|  Java 式  |      * `(expr)`    * `((Type)expr)`    * `!(expr instanceof Type)`    |
|  XML/HTML/JSP/JSPX タグ  |      * タグ    * CDATA セクション    * <% ... %>    * [Emmet](using-zen-coding-support.html)    |
|  JavaScript ステートメント  |      * `(expr)`    * `!(expr)`    * `if`    * `if / else`    * `while`    * `do / while`    * `for`    * `try / catch`    * `try / finally`    * `try / catch / finally`    * `with`    * `function`    * `{ }`    * `function expression`    |
|  PHP 文  |      * `if`    * `if / else`    * `while`    * `do / while`    * `for`    * `foreach`    * `try / catch`    * `function`    |
|  カスタム折りたたみ領域のコメント  |   囲む が適用可能なコードの任意のフラグメント。  |

