Delegates and Exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AC

    Delegates and Exceptions

    Hi All,

    I have been experimenting with delegates and exceptions.
    I know that when an exception occurs on a separate thread
    it is saved and retrieved when EndInvoke is called, but
    why can't I rethrow it?

    Regards,

    Example Code:

    class Application
    {
    delegate void MyDelegate();

    static void Main(string[] args)
    {
    Application app = new Application();
    app.Start();

    Console.ReadLin e();
    }


    public void Start()
    {
    MyDelegate myDelegate = new MyDelegate(BadM ethod);

    myDelegate.Begi nInvoke(new AsyncCallback
    (DelegateCallba ck), myDelegate);
    }


    public void BadMethod()
    {
    string newstring = string.Empty;

    if (newstring.Leng th == 0)
    {
    throw new ApplicationExce ption("delegate
    testing exception");
    }
    }


    private void DelegateCallbac k(IAsyncResult ar)
    {
    MyDelegate result = (MyDelegate) ar.AsyncState;

    try
    {
    result.EndInvok e(ar);
    }
    catch
    {
    throw;
    }
    }
    }
  • Jon Skeet [C# MVP]

    #2
    Re: Delegates and Exceptions

    AC <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
    > I have been experimenting with delegates and exceptions.
    > I know that when an exception occurs on a separate thread
    > it is saved and retrieved when EndInvoke is called, but
    > why can't I rethrow it?[/color]

    You can - what makes you think you can't?

    Note that in your example, when your exception is rethrown, nothing is
    reporting it because it's in a threadpool thread without any extra
    exception handlers.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • AC

      #3
      Re: Delegates and Exceptions

      Hi Jon,


      Thanks for the reply. How would I catch exceptions
      from the thread pool? I've tried registering a handler
      for the AppDomain.Unhan dledException to no avail.

      Thanks
      [color=blue]
      >-----Original Message-----
      >AC <anonymous@disc ussions.microso ft.com> wrote:[color=green]
      >> I have been experimenting with delegates and[/color][/color]
      exceptions.[color=blue][color=green]
      >> I know that when an exception occurs on a separate[/color][/color]
      thread[color=blue][color=green]
      >> it is saved and retrieved when EndInvoke is called,[/color][/color]
      but[color=blue][color=green]
      >> why can't I rethrow it?[/color]
      >
      >You can - what makes you think you can't?
      >
      >Note that in your example, when your exception is[/color]
      rethrown, nothing is[color=blue]
      >reporting it because it's in a threadpool thread without[/color]
      any extra[color=blue]
      >exception handlers.
      >
      >--
      >Jon Skeet - <skeet@pobox.co m>
      >http://www.pobox.com/~skeet
      >If replying to the group, please do not mail me too
      >.
      >[/color]

      Comment

      • David Levine

        #4
        Re: Delegates and Exceptions


        "AC" <anonymous@disc ussions.microso ft.com> wrote in message
        news:11d101c4f3 0a$01bc59c0$a50 1280a@phx.gbl.. .[color=blue]
        > Hi Jon,
        >
        >
        > Thanks for the reply. How would I catch exceptions
        > from the thread pool? I've tried registering a handler
        > for the AppDomain.Unhan dledException to no avail.
        >[/color]
        The threadpool itself catches and swallows all exceptions thrown on its
        threads, so you will not get an unhandled exception. You need to catch all
        the exceptions thrown in your threadpool callback routine and then
        propagate that exception using your own mechanism. You can queue it up,
        report it immediately, etc.


        Comment

        • AC

          #5
          Re: Delegates and Exceptions

          Sorry I guess I'm being thick here. But that was what I
          wanted to do in the first place. In the callback routine
          I simple wanted to throw the exception so it gets
          propagated up the call stack.

          Would it be possible to point me at a simple example?

          Thanks again.
          [color=blue]
          >-----Original Message-----
          >
          >"AC" <anonymous@disc ussions.microso ft.com> wrote in[/color]
          message[color=blue]
          >news:11d101c4f 30a$01bc59c0$a5 01280a@phx.gbl. ..[color=green]
          >> Hi Jon,
          >>
          >>
          >> Thanks for the reply. How would I catch exceptions
          >> from the thread pool? I've tried registering a handler
          >> for the AppDomain.Unhan dledException to no avail.
          >>[/color]
          >The threadpool itself catches and swallows all[/color]
          exceptions thrown on its[color=blue]
          >threads, so you will not get an unhandled exception. You[/color]
          need to catch all[color=blue]
          >the exceptions thrown in your threadpool callback[/color]
          routine and then[color=blue]
          >propagate that exception using your own mechanism. You[/color]
          can queue it up,[color=blue]
          >report it immediately, etc.
          >
          >
          >.
          >[/color]

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Delegates and Exceptions

            AC <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
            > Sorry I guess I'm being thick here. But that was what I
            > wanted to do in the first place. In the callback routine
            > I simple wanted to throw the exception so it gets
            > propagated up the call stack.
            >
            > Would it be possible to point me at a simple example?[/color]

            Which thread's stack are you expecting it to get propagated up?

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • DT

              #7
              Re: Delegates and Exceptions

              I thought that because the callback executes on the main thread in my
              example I could propagate the exception on that thread.

              I guess my new question is how to detect exceptions in the threadpool?

              Thanks again

              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1c4639 08e9d65bf398bb9 a@msnews.micros oft.com...[color=blue]
              > AC <anonymous@disc ussions.microso ft.com> wrote:[color=green]
              >> Sorry I guess I'm being thick here. But that was what I
              >> wanted to do in the first place. In the callback routine
              >> I simple wanted to throw the exception so it gets
              >> propagated up the call stack.
              >>
              >> Would it be possible to point me at a simple example?[/color]
              >
              > Which thread's stack are you expecting it to get propagated up?
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Delegates and Exceptions

                DT <dt@nospam.co.u k> wrote:[color=blue]
                > I thought that because the callback executes on the main thread in my
                > example I could propagate the exception on that thread.[/color]

                No - the callback *doesn't* execute on the main thread. It can't,
                because the main thread is busy doing something else by then - that's
                the point of it being an asynchronous call in the first place.
                [color=blue]
                > I guess my new question is how to detect exceptions in the threadpool?[/color]

                Well, you've got your callback - you can put whatever code you want in
                there, including a big try/catch round whatever else is normally there.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • DT

                  #9
                  Re: Delegates and Exceptions

                  But the callback isn't the asynch process? Isn't the whole concept of a
                  callback to notify the caller that the asynch process has completed?

                  "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                  news:MPG.1c4651 722d15242498bba 2@msnews.micros oft.com...[color=blue]
                  > DT <dt@nospam.co.u k> wrote:[color=green]
                  >> I thought that because the callback executes on the main thread in my
                  >> example I could propagate the exception on that thread.[/color]
                  >
                  > No - the callback *doesn't* execute on the main thread. It can't,
                  > because the main thread is busy doing something else by then - that's
                  > the point of it being an asynchronous call in the first place.
                  >[color=green]
                  >> I guess my new question is how to detect exceptions in the threadpool?[/color]
                  >
                  > Well, you've got your callback - you can put whatever code you want in
                  > there, including a big try/catch round whatever else is normally there.
                  >
                  > --
                  > Jon Skeet - <skeet@pobox.co m>
                  > http://www.pobox.com/~skeet
                  > If replying to the group, please do not mail me too[/color]


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Delegates and Exceptions

                    DT <dt@nospam.co.u k> wrote:[color=blue]
                    > But the callback isn't the asynch process? Isn't the whole concept of a
                    > callback to notify the caller that the asynch process has completed?[/color]

                    No, it's to allow something to happen after the main async process has
                    completed. That "something" may involve the caller, but it may not.

                    Put it this way - after your call to BeginInvoke, the main thread goes
                    on its merry way and executes some more code. What would you expect the
                    stack trace to look like in the main thread when the callback was
                    invokved? Would you expect it to just interrupt the main thread
                    wherever it was? The nightmare of re-entrancy would be horrible in that
                    case.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • DT

                      #11
                      Re: Delegates and Exceptions

                      I didn't realise that. Thanks for your help

                      Regards

                      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                      news:MPG.1c466e 5fc574be1598bba 4@msnews.micros oft.com...[color=blue]
                      > DT <dt@nospam.co.u k> wrote:[color=green]
                      >> But the callback isn't the asynch process? Isn't the whole concept of a
                      >> callback to notify the caller that the asynch process has completed?[/color]
                      >
                      > No, it's to allow something to happen after the main async process has
                      > completed. That "something" may involve the caller, but it may not.
                      >
                      > Put it this way - after your call to BeginInvoke, the main thread goes
                      > on its merry way and executes some more code. What would you expect the
                      > stack trace to look like in the main thread when the callback was
                      > invokved? Would you expect it to just interrupt the main thread
                      > wherever it was? The nightmare of re-entrancy would be horrible in that
                      > case.
                      >
                      > --
                      > Jon Skeet - <skeet@pobox.co m>
                      > http://www.pobox.com/~skeet
                      > If replying to the group, please do not mail me too[/color]


                      Comment

                      • David Levine

                        #12
                        Re: Delegates and Exceptions

                        If you just want to capture the exception information you can do something
                        like this...wrap your callback with a try-catch and in the catch block
                        simply save the exception object into a queue; this executes in the context
                        of the threadpool thread. You can then set a flag or an event to notify the
                        main thread that new data is available and it can pull the exception object
                        out of the queue.

                        "DT" <dt@nospam.co.u k> wrote in message
                        news:41dc67f6$0 $9737$65c69314@ mercury.nildram .net...[color=blue]
                        >I didn't realise that. Thanks for your help
                        >
                        > Regards
                        >
                        > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                        > news:MPG.1c466e 5fc574be1598bba 4@msnews.micros oft.com...[color=green]
                        >> DT <dt@nospam.co.u k> wrote:[color=darkred]
                        >>> But the callback isn't the asynch process? Isn't the whole concept of a
                        >>> callback to notify the caller that the asynch process has completed?[/color]
                        >>
                        >> No, it's to allow something to happen after the main async process has
                        >> completed. That "something" may involve the caller, but it may not.
                        >>
                        >> Put it this way - after your call to BeginInvoke, the main thread goes
                        >> on its merry way and executes some more code. What would you expect the
                        >> stack trace to look like in the main thread when the callback was
                        >> invokved? Would you expect it to just interrupt the main thread
                        >> wherever it was? The nightmare of re-entrancy would be horrible in that
                        >> case.
                        >>
                        >> --
                        >> Jon Skeet - <skeet@pobox.co m>
                        >> http://www.pobox.com/~skeet
                        >> If replying to the group, please do not mail me too[/color]
                        >
                        >[/color]


                        Comment

                        • DT

                          #13
                          Re: Delegates and Exceptions

                          Thanks David

                          "David Levine" <noSpam12dlevin eNNTP2@wi.rr.co m> wrote in message
                          news:uab8pN58EH A.3944@TK2MSFTN GP12.phx.gbl...[color=blue]
                          > If you just want to capture the exception information you can do something
                          > like this...wrap your callback with a try-catch and in the catch block
                          > simply save the exception object into a queue; this executes in the
                          > context of the threadpool thread. You can then set a flag or an event to
                          > notify the main thread that new data is available and it can pull the
                          > exception object out of the queue.
                          >
                          > "DT" <dt@nospam.co.u k> wrote in message
                          > news:41dc67f6$0 $9737$65c69314@ mercury.nildram .net...[color=green]
                          >>I didn't realise that. Thanks for your help
                          >>
                          >> Regards
                          >>
                          >> "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                          >> news:MPG.1c466e 5fc574be1598bba 4@msnews.micros oft.com...[color=darkred]
                          >>> DT <dt@nospam.co.u k> wrote:
                          >>>> But the callback isn't the asynch process? Isn't the whole concept of
                          >>>> a
                          >>>> callback to notify the caller that the asynch process has completed?
                          >>>
                          >>> No, it's to allow something to happen after the main async process has
                          >>> completed. That "something" may involve the caller, but it may not.
                          >>>
                          >>> Put it this way - after your call to BeginInvoke, the main thread goes
                          >>> on its merry way and executes some more code. What would you expect the
                          >>> stack trace to look like in the main thread when the callback was
                          >>> invokved? Would you expect it to just interrupt the main thread
                          >>> wherever it was? The nightmare of re-entrancy would be horrible in that
                          >>> case.
                          >>>
                          >>> --
                          >>> Jon Skeet - <skeet@pobox.co m>
                          >>> http://www.pobox.com/~skeet
                          >>> If replying to the group, please do not mail me too[/color]
                          >>
                          >>[/color]
                          >
                          >[/color]


                          Comment

                          Working...