Observable.Catch<TSource, metoda TException> (IObservable<TSource>, Func<TException, IObservable<TSource>>)
Kontynuuje obserwowaną sekwencję, która jest przerywana przez wyjątek określonego typu z obserwowaną sekwencją utworzoną przez program obsługi.
Przestrzeń nazw:System.Reactive.Linq
Zestawu: System.Reactive (w System.Reactive.dll)
Składnia
'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.
Parametry typu
- Tsource
Typ źródła.
- Wyjątek TException
Typ wyjątku.
Parametry
- source
Typ: System.IObservable<TSource>
Sekwencja źródłowa.
- Obsługi
Typ: System.Func<TException, IObservable<TSource>>
Funkcja obsługi wyjątków, tworząc kolejną obserwowaną sekwencję.
Wartość zwracana
Typ: System.IObservable<TSource>
Obserwowana sekwencja zawierająca elementy sekwencji źródłowej, po której następuje elementy utworzone przez wynikową sekwencję programu obsługi w przypadku wystąpienia wyjątku.
Uwaga dotycząca użycia
W języku Visual Basic i C# można wywołać tę metodę jako metodę wystąpienia dla dowolnego obiektu typu IObservable<TSource>. Gdy w celu wywołania tej metody jest używana składnia metody wystąpienia, należy pominąć pierwszy parametr. Aby uzyskać więcej informacji, zobacz lub .
Uwagi
Operator catch może służyć do wprowadzenia dodatkowej sekwencji do subskrypcji, gdy wystąpi wyjątek o tym samym typie co wyjątek określony w funkcji obsługi. Jest to realizowane przez operatora Catch wykonującego procedurę obsługi wyjątków, która tworzy dodatkową sekwencję. Jeśli sekwencja źródłowa jest uruchamiana do ukończenia bez wyjątku, procedura obsługi nie jest wykonywana. Jeśli napotkany wyjątek nie jest tego samego typu określony w funkcji obsługi, ten wyjątek jest wysyłany do programu obsługi OnError obserwatora. Przykładowy kod w tym temacie przedstawia operator Catch.
Przykłady
W poniższym przykładzie pokazano, jak można użyć operatora catch w celu uwzględnienia dodatkowej sekwencji liczb całkowitych z subskrypcją, jeśli wystąpi wyjątek. Zwróć uwagę, że zgłoszony wyjątek musi być tego samego typu co wyjątek w podpisie funkcji obsługi wyjątków. Jeśli nie jest to ten sam typ, procedura obsługi OnError dla obserwatora jest wykonywana zamiast obsługi wyjątków.
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();
}
}
}
Poniżej przedstawiono przykładowe dane wyjściowe z przykładowego kodu.
==============================================================
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...