For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/stage/4/proposal-optional-catch-binding.md.
  • 简体中文
  • Optional catch binding S4

    中文标题:可选 catch 绑定

    提案概览
    提案速览

    此提案解决了在故意忽略错误的场景中,常见的使用未使用 catch 绑定(例如 catch (unused))的做法,这种做法可能暗示编程错误。它对 ECMAScript 引入了语法变更,允许省略 catch 绑定及其圆括号,从而支持 catch { . } 这样的语法。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    此提案对 ECMAScript 做出语法变更,允许在不需要使用 catch 绑定的情况下省略该绑定。这种情况在诸如

    try {
      // 尝试使用可能未被实现的 Web 功能
    } catch (unused) {
      // 回退到支持更广泛的、较不理想的 Web 功能
    }

    let isTheFeatureImplemented = false;
    try {
      // 测试 Web API 的必要部分
      isTheFeatureImplemented = true;
    } catch (unused) {}

    let parseResult = someFallbackValue;
    try {
      parseResult = JSON.parse(potentiallyMalformedJSON);
    } catch (unused) {}

    等模式中频繁出现,且通常认为声明或写入但从未读取的变量表示编程错误。

    此提案引入的语法变更允许省略 catch 绑定及其周围的圆括号,如

    try {
      // ...
    } catch {
      // ...
    }

    更多信息请参阅提案全文