> ## Documentation Index
> Fetch the complete documentation index at: https://docs.runanywhere.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Error Handling

> SDKException, codes, and recovery

Everything the SDK throws is an `SDKException`, wrapping a proto-backed error so the same code
and category cross every language binding.

```dart theme={"theme":"css-variables"}
try {
  final result = await RunAnywhere.llm.generate(prompt);
} on SDKException catch (e) {
  print(e.code);
  print(e.message);
}
```

## Codes worth handling

| Code | Meaning | What to do |
| - | - | - |
| `notInitialized` | Called before `initialize()` | Initialize first |
| `modelNotFound` | No such model | Check the id, or register it |
| `networkUnavailable` | Download needs a connection | Retry when online |
| `insufficientStorage` | Not enough disk | Free space or use a smaller model |
| `insufficientMemory` | Model will not fit in RAM | Unload something first |
| `microphonePermissionDenied` | No mic access | Send the user to Settings |
| `timeout` | Took too long | Retry |
| `invalidApiKey` | Credentials rejected | Check the key |
| `cancelled` | The caller cancelled | Usually ignore |
| `invalidState` | Wrong order of operations | Read the message |
| `featureNotAvailable` | Not implemented in this build | See below |

## featureNotAvailable

Two cases you will actually meet:

```dart theme={"theme":"css-variables"}
// Throws: constrained decoding is not wired in
await RunAnywhere.llm.generateStructured(prompt, schema,
    mode: StructuredOutputMode.constrained);
```

```dart theme={"theme":"css-variables"}
// Throws invalidState: the RAG index is single-session
final a = await RunAnywhere.rag.open(embeddingModel: model);
final b = await RunAnywhere.rag.open(embeddingModel: model);
// "A RAG session is already open; close it before opening another"
```

> [!NOTE]
>
> Flutter names this factory `featureNotAvailable`, where Kotlin calls it `unsupportedCapability`.
> Same condition, different name.

## deviceId throws

```dart theme={"theme":"css-variables"}
// Throws SDKException before initialize()
final id = RunAnywhere.deviceId;
```

Guard on `isReady`:

```dart theme={"theme":"css-variables"}
final id = RunAnywhere.isReady ? RunAnywhere.deviceId : null;
```

## Errors from a Stream

Failures arrive through the stream's error channel:

```dart theme={"theme":"css-variables"}
try {
  await for (final event in RunAnywhere.llm.generateStream(prompt)) {
    // …
  }
} on SDKException catch (e) {
  show(e);
}
```

Or with a subscription:

```dart theme={"theme":"css-variables"}
RunAnywhere.llm.generateStream(prompt).listen(
  handleEvent,
  onError: (Object error) {
    if (error is SDKException) show(error);
  },
);
```

`onError` is easy to forget on a subscription, and an unhandled stream error becomes an
uncaught async exception rather than a crash you can see.

## Cancellation

Cancelling a subscription cancels the generation and is not an error.

```dart theme={"theme":"css-variables"}
await _subscription?.cancel();
```

## Checking mounted

Every `await` in a `State` is a place the widget may disappear:

```dart theme={"theme":"css-variables"}
final result = await RunAnywhere.llm.generate(prompt);
if (!mounted) return;
setState(() => _text = result.text);
```

## Reporting

```dart theme={"theme":"css-variables"}
on SDKException catch (e) {
  analytics.logEvent('sdk_error', {'code': e.code.toString()});
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.