SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method Throw

    | Edit this page

    Throw<TSource>(Exception)

    Returns a sequence that throws an exception upon enumeration.

    Declaration
    public static IEnumerable<TSource> Throw<TSource>(Exception exception)
    Parameters
    Type Name Description
    Exception exception

    Exception to throw upon enumerating the resulting sequence.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence that throws the specified exception upon enumeration.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    The provided value (exception) will be thrown when the first element is enumerated.

    If the returned IEnumerable<T> is enumerated multiple times, the same value will be thrown each time.

    Examples

    The following code example demonstrates how to create a sequence that throws on enumeration using Throw.

    // this sequence will throw an exception on the 6th element
    var sequence = Enumerable.Range(1, 5).Select(i => i.ToString())
        .Concat(SuperEnumerable.Throw<string>(new InvalidOperationException()));
    
    // Use a function to determine how to handle an exception
    var result = sequence
        .Catch(
            (InvalidOperationException ex) => SuperEnumerable.Return(ex.Message));
    
    Console.WriteLine(
        "[" + 
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 3, 4, 5, Operation is not valid due to the current state of the object.]
    
    Exceptions
    Type Condition
    ArgumentNullException

    exception is null.

    © SuperLinq Authors. All rights reserved.