Observable.Catch<TSource、TException> メソッド (IObservable<TSource>、Func<TException、IObservable<TSource>>)

ハンドラーによって生成された監視可能なシーケンスを使用して、指定された型の例外によって終了される監視可能なシーケンスを続行します。

Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'Declaration
<ExtensionAttribute> _
Public Shared Function Catch(Of TSource, TException As Exception) ( _
    source As IObservable(Of TSource), _
    handler As Func(Of TException, IObservable(Of TSource)) _
) As IObservable(Of TSource)
'Usage
Dim source As IObservable(Of TSource)
Dim handler As Func(Of TException, IObservable(Of TSource))
Dim returnValue As IObservable(Of TSource)

returnValue = source.Catch(handler)
public static IObservable<TSource> Catch<TSource, TException>(
    this IObservable<TSource> source,
    Func<TException, IObservable<TSource>> handler
)
where TException : Exception
[ExtensionAttribute]
public:
generic<typename TSource, typename TException>
where TException : Exception
static IObservable<TSource>^ Catch(
    IObservable<TSource>^ source, 
    Func<TException, IObservable<TSource>^>^ handler
)
static member Catch : 
        source:IObservable<'TSource> * 
        handler:Func<'TException, IObservable<'TSource>> -> IObservable<'TSource>  when 'TException : Exception
JScript does not support generic types and methods.

型パラメーター

  • TSource
    ソースの種類。
  • TException
    例外の型。

パラメーター

  • handler
    種類: System.Func<TException、 IObservable<TSource>>
    別の監視可能なシーケンスを生成する例外ハンドラー関数。

戻り値

種類: System.IObservable<TSource>
ソース シーケンスの要素を含む監視可能なシーケンス。例外が発生した場合にハンドラーの結果の監視可能なシーケンスによって生成される要素が続きます。

使用上の注意

Visual Basic および C# では、 IObservable<TSource> 型の任意のオブジェクトでインスタンス メソッドとしてこのメソッドを呼び出すことができます。 インスタンス メソッド構文を使用してこのメソッドを呼び出す場合は、最初のパラメーターを省略します。 詳細については、」または」を参照してください。

解説

catch 演算子を使用すると、ハンドラー関数で指定された例外と同じ種類の例外が発生したときに、サブスクリプションに追加のシーケンスを導入できます。 これは、追加のシーケンスを生成する例外ハンドラーを実行する Catch 演算子によって実現されます。 ソース シーケンスが例外なしで完了まで実行される場合、ハンドラーは実行されません。 発生した例外がハンドラー関数で指定された型ではない場合、その例外はオブザーバーの OnError ハンドラーに送信されます。 このトピックのコード例では、Catch 演算子を示します。

次の例では、例外が発生した場合に catch 演算子を使用して、サブスクリプションに整数の追加シーケンスを含める方法を示します。 スローされる例外は、例外ハンドラー関数のシグネチャの例外と同じ型である必要があることに注意してください。 同じ型でない場合は、例外ハンドラーではなく、オブザーバーの OnError ハンドラーが実行されます。

using System;
using System.Collections.Generic;
using System.Reactive.Linq;

namespace Example
{

  class Program
  {

    static void Main()
    {
      //***********************************************************************************************//
      //*** sequence1 is generated from the enumerator returned by the RandomNumSequence function.  ***//
      //*** It will be combined with sequence2 using the Catch() operator only if there is an       ***//
      //*** exception thrown in sequence1 that is caught by the Catch() operator.                   ***//
      //***********************************************************************************************//
      IObservable<int> sequence1 = RandomNumSequence().ToObservable();


      //**************************************************************************************************************************//
      //*** In this catch operation, the exception handler for Observable::Catch will not be executed. This is because         ***//
      //*** sequence1 throws an InvalidOperationException which isn't of the NullReferenceException type specified in the      ***//
      //*** signature of ExNullRefHandler.                                                                                     ***//
      //***                                                                                                                    ***//
      //*** The InvalidOperationException will be caught by the OnError handler instead.                                       ***//
      //**************************************************************************************************************************//
      Console.WriteLine("==============================================================");
      Console.WriteLine("Calling Catch operator with NullReferenceException handler...");
      Console.WriteLine("==============================================================\n");
      sequence1.Catch((Func<NullReferenceException, IObservable<int>>)ExNullRefHandler)
        .Subscribe(i => Console.WriteLine(i),
                  (ex) => Console.WriteLine("\nException {0} in OnError handler\nException.Message : \"{1}\"\n\n", ex.GetType().ToString(), ex.Message));


      //**************************************************************************************************************************//
      //*** In this catch operation, the exception handler will be executed. Because InvalidOperationException thrown by       ***//
      //*** sequence1 is of the InvalidOperationException type specified in the signature of ExInvalidOpHandler().             ***//
      //**************************************************************************************************************************//

        Console.WriteLine("================================================================");
        Console.WriteLine("Calling Catch operator with InvalidOperationException handler...");
        Console.WriteLine("================================================================\n");
        sequence1.Catch((Func<InvalidOperationException, IObservable<int>>)ExInvalidOpHandler)
        .Subscribe(i => Console.WriteLine(i),
                  (ex) => Console.WriteLine("\nException {0} in OnError handler\nException.Message : \"{1}\"\n\n", ex.GetType().ToString(), ex.Message));


      Console.WriteLine("\nPress ENTER to exit...\n");
      Console.ReadLine();
    }




    //*******************************************************************************************************//
    //***                                                                                                 ***//
    //*** This method will yield a random sequence of 5 integers then throw an InvalidOperationException. ***//
    //***                                                                                                 ***//
    //*******************************************************************************************************//
    static IEnumerable<int> RandomNumSequence()
    {
      Random random = new Random();

      //************************************************//
      //*** Yield an a sequence of 5 random integers ***//
      //************************************************//
      for (int i = 0; i < 5; i++)
      {
        yield return random.Next(101);
      }

      //*********************************************************//
      //*** Then throw an InvalidOperationException exception ***//
      //*********************************************************//
      throw new InvalidOperationException("Some Exception Happened!");
    }



    //*********************************************************************************************************//
    //***                                                                                                   ***//
    //*** Simple catch handler for NullReferenceExceptions. This handler looks at the exception message and ***//
    //*** returns a sequence of int.                                                                        ***//
    //***                                                                                                   ***//
    //*********************************************************************************************************//
    static IObservable<int> ExNullRefHandler(NullReferenceException ex)
    {
      //***********************************************************************************************//
      //*** Sequence2 will be part of the resulting sequence if an exception is caught in sequence1 ***//
      //***********************************************************************************************//
      int[] sequence2 = { 0 };

      if (ex.Message == "Some Exception Happened!")
        sequence2 = new int[] { 5001, 5002, 5003, 5004 };

      return sequence2.ToObservable();
    }



    //************************************************************************************************************//
    //***                                                                                                      ***//
    //*** Simple catch handler for InvalidOperationExceptions. This handler looks at the exception message and ***//
    //*** returns a sequence of int.                                                                           ***//
    //***                                                                                                      ***//
    //************************************************************************************************************//
    static IObservable<int> ExInvalidOpHandler(InvalidOperationException ex)
    {
      //***********************************************************************************************//
      //*** Sequence2 will be part of the resulting sequence if an exception is caught in sequence1 ***//
      //***********************************************************************************************//
      int[] sequence2 = { 0 };

      if (ex.Message == "Some Exception Happened!")
        sequence2 = new int[] { 1001, 1002, 1003, 1004 };

      return sequence2.ToObservable();
    }
  }
}

コード例からの出力例を次に示します。

==============================================================
Calling Catch operator with NullReferenceException handler...
==============================================================

68
20
17
6
24

Exception System.InvalidOperationException in OnError handler
Exception.Message : "Some Exception Happened!"


================================================================
Calling Catch operator with InvalidOperationException handler...
================================================================

87
29
84
68
23
1001
1002
1003
1004

Press ENTER to exit...

参照

リファレンス

Observable クラス

Catch オーバーロード

System.Reactive.Linq 名前空間