# 包围代码片段

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com.cn/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.cn/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))），转到  `编辑器 | 文件和代码模板` 。

例如，您可以配置 final 修饰符、名称甚至异常的类型，以用于  `try...catch` 条件语句。

![代码模板设置](https://resources.jetbrains.com.cn/help/img/idea/2026.3/code_template.png)

> **Note:**
> 如果 实时模板 包含 [$SELECTION$ variable](template-variables.html#pdtv)，它们可以用作环绕语句。
>
>
>
> 要创建您自己的环绕语句， [创建一个 实时模板](creating-and-editing-live-templates.html) 并在模板文本中使用  `$选择$`  变量。

## 示例

在以下示例中，我们选择  `int number = scanner.nextInt();`  Java 语句，并应用   try / catch   包围语句。

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`    * `函数`    * `{ }`    * `函数表达式`    |
|  PHP 语句  |      * `if`    * `if / else`    * `while`    * `do / while`    * `for`    * `foreach`    * `try / catch`    * `函数`    |
|  自定义折叠区域注释  |  任何可应用  包围方式  的代码片段。  |

