# Errors and exceptions

Source: https://libtmux.org/en/csharp/latest/topics/errors-and-exceptions/

> Handle command failures, inspect delivery status, and decide when a retry is safe.

A command can fail because tmux rejects it, or because the transport stops
before returning a reply. Inspect the reported error and delivery state before
retrying a mutation: tmux may already have received it.

For lookup failures caused by zero or multiple matches, see [Filtering and
queries](https://libtmux.org/en/csharp/latest/concepts/queries/#the-cardinality-contract-side-by-side).

## A failed command, as a value

Failures raise subclasses of [`LibTmuxException`](<https://libtmux.org/en/csharp/latest/reference/libtmux-libtmuxexception/>), including
[`TmuxCommandException`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxcommandexception/>), [`TmuxTransportException`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxtransportexception/>), and
[`TmuxObjectNotFoundException`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxobjectnotfoundexception/>).

## Is it safe to retry?

Retry a mutation automatically only when you know it was not dispatched, or when
repeating it is safe for your operation. A timeout, cancellation, or dropped
connection can occur after tmux has acted. These APIs expose delivery
information:

[`LibTmuxException.Dispatch`](<https://libtmux.org/en/csharp/latest/reference/libtmux-libtmuxexception-dispatch/>) reports a [`TmuxDispatchState`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxdispatchstate/>):
[`NotDispatched`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxdispatchstate-notdispatched/>), [`Dispatched`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxdispatchstate-dispatched/>), or [`Unknown`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxdispatchstate-unknown/>). Treat the default, [`Unknown`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxdispatchstate-unknown/>),
as potentially dispatched.

```csharp
try
{
    await session.CreateWindowAsync(new NewWindowRequest(name: "build"));
}
catch (LibTmuxException error) when (error.Dispatch == TmuxDispatchState.NotDispatched)
{
    // safe to retry
}
```

Treat unknown delivery as potentially executed.

[`NotDispatched`](<https://libtmux.org/en/csharp/latest/reference/libtmux-tmuxdispatchstate-notdispatched/>) identifies a request that did not reach tmux.

For subprocess calls that complete normally, inspect the exit status. If a call
is interrupted or times out without a delivery state, check tmux's resulting
state before repeating a mutation.
