TheAlgorithms/C-Sharp · error · InvalidOperationException
Deque is empty.
Error message
Deque is empty.
What it means
RemoveFront() throws InvalidOperationException when the deque has no elements. The library validates IsEmpty before accessing items[front] because a circular-array deque has no sentinel object to return when Count == 0. It is a deliberate fail-fast contract: removing from an empty deque is a caller bug, not a recoverable condition.
Solutions
- Check the IsEmpty property (or Count == 0) before calling RemoveFront()
- Wrap the call in try-catch for InvalidOperationException if emptiness is expected and recoverable
- Use PeekFront() behind an IsEmpty check to inspect without removing
- Fix producer/consumer balance so consumers never out-drain producers
Example fix
// before T item = deque.RemoveFront(); // after if (deque.IsEmpty) return; T item = deque.RemoveFront();
Defensive patterns
Strategy: try-catch
Validate before calling
if (deque == null) throw new ArgumentNullException(nameof(deque));
if (deque.IsEmpty)
return; // or default(T) / Optional<T>.None
T item = deque.RemoveFront(); Type guard
bool CanRemoveFront<T>(Deque<T> d) => d != null && !d.IsEmpty;
Try / catch
try
{
T item = deque.RemoveFront();
Process(item);
}
catch (InvalidOperationException ex) when (ex.Message == "Deque is empty.")
{
// deque drained; handle empty case
} Prevention
- Always check IsEmpty or Count > 0 before any Remove/Peek call
- In drain loops use while (!deque.IsEmpty) { ... RemoveFront(); }
- For shared deques, re-check emptiness immediately before removal (or lock)
- Prefer PeekFront with an IsEmpty guard when the element must survive a failed removal
When it happens
Trigger: Calling RemoveFront() when Count == 0, i.e. on a newly constructed Deque<T>, after removing all elements (front and rear drained to empty), or calling it a second time after a prior RemoveFront/RemoveRear emptied the deque.
Common situations: Looping 'while' over a deque draining elements without checking Count/IsEmpty; off-by-one bookkeeping where the consumer removes one more item than was added; shared deque consumed by multiple callers where another consumer emptied it first.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- Tree is empty!
- Capacity must be at least 1.
- There are no items in the queue.
- The queue contains no items.
- Tree is empty!
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/63cc5b6a1ddd686e.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Deque/Deque.cs:139
count++;
}
/// <summary>
/// Removes and returns the element at the front of the deque.
/// This operation is O(1) time complexity.
/// </summary>
/// <returns>The element at the front of the deque.</returns>
/// <exception cref="InvalidOperationException">Thrown when the deque is empty.</exception>
/// <example>
/// // Deque: [3, 5, 7].
/// int value = deque.RemoveFront(); // Returns 3, Deque: [5, 7].
/// </example>
public T RemoveFront()
{
// Validate that deque is not empty
if (IsEmpty)
{
throw new InvalidOperationException("Deque is empty.");
}
// Retrieve the front element
T item = items[front];
// Clear the reference to help garbage collection
items[front] = default!;
// Move front pointer forward in circular fashion
front = (front + 1) % items.Length;
count--;
return item;
}
/// <summary>
/// Removes and returns the element at the rear of the deque.
/// This operation is O(1) time complexity.View on GitHub (pinned to 96e2905cab)