SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method Catch

    | Edit this page

    Catch<TSource, TException>(IEnumerable<TSource>, Func<TException, IEnumerable<TSource>>)

    Creates a sequence that corresponds to the source sequence, concatenating it with the sequence resulting from calling an exception handler function in case of an error.

    Declaration
    public static IEnumerable<TSource> Catch<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler) where TException : Exception
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    Source sequence.

    Func<TException, IEnumerable<TSource>> handler

    Handler to invoke when an exception of the specified type occurs.

    Returns
    Type Description
    IEnumerable<TSource>

    Source sequence, concatenated with an exception handler result sequence in case of an error.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    TException

    Exception type to catch.

    Remarks

    This method uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to use the Catch operator.

    // 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

    source or handler is null.

    ArgumentNullException

    (Thrown lazily) The sequence errSource returned by handler is null.

    | Edit this page

    Catch<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)

    Creates a sequence that returns the elements of the first sequence, switching to the second in case of an error.

    Declaration
    public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
    Parameters
    Type Name Description
    IEnumerable<TSource> first

    First sequence.

    IEnumerable<TSource> second

    Second sequence, concatenated to the result in case the first sequence completes exceptionally.

    Returns
    Type Description
    IEnumerable<TSource>

    The first sequence, followed by the second sequence in case an error is produced.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    This method uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to use the Catch operator.

    // this sequence will throw an exception on the 6th element
    var sequence = Enumerable.Range(1, 5)
        .Concat(SuperEnumerable.Throw<int>(new InvalidOperationException()));
    
    // Use a second sequence to continue in the case of an exception
    var result = sequence
        .Catch(Enumerable.Range(1, 5));
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
    
    Exceptions
    Type Condition
    ArgumentNullException

    first or second is null.

    | Edit this page

    Catch<TSource>(params IEnumerable<TSource>[])

    Creates a sequence by concatenating source sequences until a source sequence completes successfully.

    Declaration
    public static IEnumerable<TSource> Catch<TSource>(params IEnumerable<TSource>[] sources)
    Parameters
    Type Name Description
    IEnumerable<TSource>[] sources

    Source sequences.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence that continues to concatenate source sequences while errors occur.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    This method uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to use the Catch operator.

    // this sequence will throw an exception on the 6th element
    var sequence = Enumerable.Range(1, 5)
        .Concat(SuperEnumerable.Throw<int>(new InvalidOperationException()));
    
    // Use a series of sequences to enumerate until one completes successfully.
    var result = SuperEnumerable
        .Catch(
            sequence, 
            sequence,
            Enumerable.Range(1, 3), 
            Enumerable.Range(1, 10));
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3]
    
    Exceptions
    Type Condition
    ArgumentNullException

    sources is null.

    ArgumentNullException

    (Thrown lazily) Any sequence source returned by sources is null.

    | Edit this page

    Catch<TSource>(IEnumerable<IEnumerable<TSource>>)

    Creates a sequence by concatenating source sequences until a source sequence completes successfully.

    Declaration
    public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
    Parameters
    Type Name Description
    IEnumerable<IEnumerable<TSource>> sources

    Source sequences.

    Returns
    Type Description
    IEnumerable<TSource>

    Sequence that continues to concatenate source sequences while errors occur.

    Type Parameters
    Name Description
    TSource

    Source sequence element type.

    Remarks

    This method uses deferred execution and streams its results.

    Examples

    The following code example demonstrates how to use the Catch operator.

    // this sequence will throw an exception on the 6th element
    var sequence = Enumerable.Range(1, 5)
        .Concat(SuperEnumerable.Throw<int>(new InvalidOperationException()));
    
    // Use a series of sequences to enumerate until one completes successfully.
    var result = SuperEnumerable
        .Catch(
            new List<IEnumerable<int>>
            {
                sequence,
                sequence,
                Enumerable.Range(1, 3),
                Enumerable.Range(1, 10),
            });
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3]
    
    Exceptions
    Type Condition
    ArgumentNullException

    sources is null.

    ArgumentNullException

    (Thrown lazily) Any sequence source returned by sources is null.

    © SuperLinq Authors. All rights reserved.