# error_handling

URL: /kida/api/compiler/statements/error_handling/
Section: statements
Description: Error boundary compilation for Kida compiler.

Compiles {% try %}...{% fallback %}...{% end %} to Python try/except
with sub-buffer management for streaming safety.

Uses inline TYPE_CHECKING declarations for host attributes.
See: plan/rfc-mixin-protocol-typing.md

---

> For a complete page index, fetch /kida/llms.txt.

Open LLM text
(/kida/api/compiler/statements/error_handling/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Ferror_handling%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Ferror_handling%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Ferror_handling%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fkida%2Fapi%2Fcompiler%2Fstatements%2Ferror_handling%2Findex.txt)

Module

#
`compiler.statements.error_handling`

Error boundary compilation for Kida compiler.

Compiles {% try %}...{% fallback %}...{% end %} to Python try/except
with sub-buffer management for streaming safety.

Uses inline TYPE_CHECKING declarations for host attributes.
See: plan/rfc-mixin-protocol-typing.md

1Class

## Classes

`ErrorHandlingMixin`

1

▼

Mixin for compiling error boundary statements.

Host attributes and cross-mixin dependencies are de…

Mixin for compiling error boundary statements.

Host attributes and cross-mixin dependencies are declared via inline
TYPE_CHECKING blocks.

#### Methods

Internal Methods
1

▼

`_compile_try`

1

`list[ast.stmt]`

▼

Compile {% try %}...{% fallback %}...{% end %} error boundary.

Both StringBuil…

`def _compile_try(self, node: Try) -> list[ast.stmt]`

Compile {% try %}...{% fallback %}...{% end %} error boundary.

Both StringBuilder and streaming modes use the same sub-buffer
strategy for the try body (must buffer to discard on error).

Generated code (StringBuilder mode):

```
_try_buf_N = []
_try_append_N = _try_buf_N.append
_saved_append_N = _append
_append = _try_append_N
try:
    # body compiled with _append → sub-buffer
    _append = _saved_append_N
    _append(''.join(_try_buf_N))
except (...) as _err_N:
    _append = _saved_append_N
    # fallback body
```

Generated code (streaming mode):

```
_try_buf_N = []
_append = _try_buf_N.append
try:
    # body compiled in non-streaming mode → _append to buffer
    for _chunk in _try_buf_N: yield _chunk
except (...) as _err_N:
    # fallback body compiled in streaming mode (yields)
```

##### Parameters

Name
Type
Description

`node`
`—`

##### Returns

`list[ast.stmt]`
