共用方式為


Observable.Catch < TSource, TException > 方法 (IObservable < TSource > , Func < TException, IObservable < TSource >>)

使用處理程式所產生的可觀察序列,繼續以指定型別的例外狀況終止的可觀察序列。

Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (

Syntax

'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 運算子如何使用 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 命名空間