Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3495196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:05:51+00:00 2026-05-18T12:05:51+00:00

I have the following scenario. A Task that generates events and might throw an

  • 0

I have the following scenario. A Task that generates events and might throw an exception:

public event EventHandler<EventArgs> MyEvent;
new Task(() =>
    {
        while (condition)
        {
            // Generate standard .NET event.
            MyEvent(this, new EventArgs());

            // Maybe throw exception.
            if (somethingIsWrong) throw new Exception();
        }
    });

All pretty straightforward. I listen to the events using Observable.FromEvent:

var events =
    Observable.FromEvent<EventArgs>(h => myClass.MyEvent += h,
                                    h => myClass.MyEvent -= h);
events.Subscribe(
    ev => DoSomethingOnNext(ev),
    ex => DoSomethingOnError(ex),
    () => DoSomethingOnCompleted());

This all works fine when no exception occurs. When an exception is thrown by the task, however, I’d like to know this in my observable. The exception is now ‘hidden’ inside the task.

Can I only do this by creating another event when the exception takes place, wrapping it inside an IObservable and subscribing to this new observable? Or is there a simpler way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:05:51+00:00Added an answer on May 18, 2026 at 12:05 pm

    How about this:

    class Program
    {
        public event EventHandler<EventArgs> MyEvent;
        static void Main(string[] args)
        {
            var myClass = new Program();            
    
            var task = new Task(() =>
            {
                for(var i=0; i<5; i++) {
                    // Generate standard .NET event. 
                    myClass.MyEvent(myClass, new EventArgs());
                }
    
                throw new Exception();
            });
    
            var obsTask = task.ToObservable();
    
            var events = Observable.FromEvent<EventArgs>(h => myClass.MyEvent += h, h => myClass.MyEvent -= h);            
    
            events.TakeUntil(obsTask).Subscribe(
                ev => DoSomethingOnNext(ev), 
                ex => DoSomethingOnError(ex),
                () => DoSomethingOnCompleted());
    
            task.Start();
    
            Console.ReadKey();
        }
    
        private static void DoSomethingOnCompleted()
        {
            Console.WriteLine("DoSomethingOnCompleted");
        }
    
        private static void DoSomethingOnError(Exception ex)
        {
            Console.WriteLine("DoSomethingOnError:" + ex.ToString());
        }
    
        private static void DoSomethingOnNext(IEvent<EventArgs> ev)
        {
            Console.WriteLine("DoSomethingOnNext:" + ev.ToString());
        }
    

    The output is:

    DoSomethingOnNext:System.Collections.Generic.Event1[System.EventArgs]
    DoSomethingOnNext:System.Collections.Generic.Event
    1[System.EventArgs]
    DoSomethingOnNext:System.Collections.Generic.Event1[System.EventArgs]
    DoSomethingOnNext:System.Collections.Generic.Event
    1[System.EventArgs]
    DoSomethingOnNext:System.Collections.Generic.Event`1[System.EventArgs]
    DoSomethingOnError:System.AggregateException: One or more errors occurred. —> System.Exception: Exception of type ‘System.Exception’ was thrown.
    at RxDisposeTests.Program.<>c_DisplayClass9.b_0() in C:\Users\Richard.Hein\Documents\Visual Studio 2010\Projects\RxConsole\RxDisposeTests\Program.cs:line 25
    at System.Threading.Tasks.Task.InnerInvoke()
    at System.Threading.Tasks.Task.Execute()
    — End of inner exception stack trace —
    —> (Inner Exception #0) System.Exception: Exception of type ‘System.Exception’ was thrown.
    at RxDisposeTests.Program.<>c_DisplayClass9.b_0() in C:\Users\Richard.Hein\Documents\Visual Studio 2010\Projects\RxConsole\RxDisposeTests\Program.cs:line 25
    at System.Threading.Tasks.Task.InnerInvoke()
    at System.Threading.Tasks.Task.Execute()<—

    EDIT:

    Not sure if TakeUntil is a good solution, because Task might return something other than Exceptions, right? So this could work:

    var events = Observable.CreateWithDisposable<IEvent<EventArgs>>(observer =>
    {
        var eventObs = Observable.FromEvent<EventArgs>(
            h => myClass.MyEvent += h, h => myClass.MyEvent -= h);
        task.ToObservable().Subscribe(_ => { }, observer.OnError, observer.OnCompleted);
        return eventObs.Subscribe(observer.OnNext, observer.OnError, observer.OnCompleted);
    });
    
    events.Subscribe(
        ev => DoSomethingOnNext(ev), 
        ex => DoSomethingOnError(ex),
        () => DoSomethingOnCompleted());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scenario :- template <typename T> class Base { public: virtual
I have this following scenario namespace A { //... class A { public: static
I have the following scenario that may warrant storing data in a conroller member
I have the following scenario: class Task { int id; Group group; User user;
Lets say i have the following scenario: public class A { public String createString(final
The scenario is the following: I have a java package ( myapp.config ) that
I have the following scenario public class Foo { public Bar FooBar { get;
I have an EditTask View for editing the following properties for a Task that
I have the following Android scenario: my activity launches an asynchronous task to perform
I am trying to understand isolation/locks in SQL Server. I have following scenario in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.