Exceptions throughout multiple classes

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

    Exceptions throughout multiple classes

    Hi all,

    The title of this post may sound a bit weird, but I was wondering about the
    following nonetheless.

    I have a class libray containing, say, 4 classes: A, B, C, D. Class A
    somehow has a reference to B, B has a reference to C, and C to D.
    If an exception happens in class D, I would like class A to get a
    notification of this (all execution on classes B to D should be terminated).
    I am wondering how to do this. The following seems like a bad idea:

    class D {
    {
    try(...)
    catch(SomeExcep tion ex)
    throw new SomeException(" Error in D");
    }

    class C
    {
    D d = new D();
    try
    { d.doSomething() ; }
    catch(SomeExcep tion ex)
    { throw new SomeException(e x.Message); }
    }

    and catch THAT exception in B, and throw it to A. I think you get the idea
    :) I'm quite sure this is a bad idea. But how should I structure it then? No
    error handling at all in classes B to D is even worse. I hope someone can
    give me a good idea.

    Thanks!

    Razzie


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Exceptions throughout multiple classes

    Razzie,

    If you want to invalidate the classes when an exception occurs, then you
    will have to use the try/catch block and set a flag indicating that the
    class can not be used. You would then check that flag for every operation
    on that class.

    I would say that this is a bit overkill, plus, you are not designing
    your classes to be resilient enough in the face of these exceptions.

    Also, are you throwing exceptions as a result of say, errors in logic?
    If so, you might want to reconsider using exceptions (is the error in logic
    truly an exceptional case) and use some sort of return value,

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Razzie" <razzie@quickne t.nl> wrote in message
    news:uoKDrenwEH A.3724@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hi all,
    >
    > The title of this post may sound a bit weird, but I was wondering about
    > the following nonetheless.
    >
    > I have a class libray containing, say, 4 classes: A, B, C, D. Class A
    > somehow has a reference to B, B has a reference to C, and C to D.
    > If an exception happens in class D, I would like class A to get a
    > notification of this (all execution on classes B to D should be
    > terminated). I am wondering how to do this. The following seems like a bad
    > idea:
    >
    > class D {
    > {
    > try(...)
    > catch(SomeExcep tion ex)
    > throw new SomeException(" Error in D");
    > }
    >
    > class C
    > {
    > D d = new D();
    > try
    > { d.doSomething() ; }
    > catch(SomeExcep tion ex)
    > { throw new SomeException(e x.Message); }
    > }
    >
    > and catch THAT exception in B, and throw it to A. I think you get the idea
    > :) I'm quite sure this is a bad idea. But how should I structure it then?
    > No error handling at all in classes B to D is even worse. I hope someone
    > can give me a good idea.
    >
    > Thanks!
    >
    > Razzie
    >[/color]


    Comment

    • Razzie

      #3
      Re: Exceptions throughout multiple classes

      Hello Nicholas,

      Some classes use so-called template files to read data from. Exceptions
      could occur when a template file is missing, has invalid data, etc. That
      should never happen but IF it happens, execution cannot continue.
      I was really wondering if catching and rethrowing an exception is very bad
      performance-wise (I guess it is, since catching costs performance) so I
      don't want to go that way.
      I've been thinking of returning some value myself, for example return null
      instead of an object and check if an object is null ---> something must have
      gone wrong. But I'd have to use THAT throughout class D to A, and I was
      wondering if there was just one 'fast' way.
      Isn't it possible to use events for this? If an exception in D happens, an
      event is raised in A...?

      Thanks,

      Razzie

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:OcinejnwEH A.4040@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Razzie,
      >
      > If you want to invalidate the classes when an exception occurs, then
      > you will have to use the try/catch block and set a flag indicating that
      > the class can not be used. You would then check that flag for every
      > operation on that class.
      >
      > I would say that this is a bit overkill, plus, you are not designing
      > your classes to be resilient enough in the face of these exceptions.
      >
      > Also, are you throwing exceptions as a result of say, errors in logic?
      > If so, you might want to reconsider using exceptions (is the error in
      > logic truly an exceptional case) and use some sort of return value,
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Razzie" <razzie@quickne t.nl> wrote in message
      > news:uoKDrenwEH A.3724@TK2MSFTN GP10.phx.gbl...[color=green]
      >> Hi all,
      >>
      >> The title of this post may sound a bit weird, but I was wondering about
      >> the following nonetheless.
      >>
      >> I have a class libray containing, say, 4 classes: A, B, C, D. Class A
      >> somehow has a reference to B, B has a reference to C, and C to D.
      >> If an exception happens in class D, I would like class A to get a
      >> notification of this (all execution on classes B to D should be
      >> terminated). I am wondering how to do this. The following seems like a
      >> bad idea:
      >>
      >> class D {
      >> {
      >> try(...)
      >> catch(SomeExcep tion ex)
      >> throw new SomeException(" Error in D");
      >> }
      >>
      >> class C
      >> {
      >> D d = new D();
      >> try
      >> { d.doSomething() ; }
      >> catch(SomeExcep tion ex)
      >> { throw new SomeException(e x.Message); }
      >> }
      >>
      >> and catch THAT exception in B, and throw it to A. I think you get the
      >> idea :) I'm quite sure this is a bad idea. But how should I structure it
      >> then? No error handling at all in classes B to D is even worse. I hope
      >> someone can give me a good idea.
      >>
      >> Thanks!
      >>
      >> Razzie
      >>[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Exceptions throughout multiple classes

        Razzie,

        You could use events, but that seems like a lot of work as well.

        If D uses C and C uses B and B uses A, why not have the process that
        makes calls on D just end? I mean, why not just ditch that process and then
        start over, discarding the chain?


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Razzie" <razzie@quickne t.nl> wrote in message
        news:eGIl65nwEH A.3808@TK2MSFTN GP15.phx.gbl...[color=blue]
        > Hello Nicholas,
        >
        > Some classes use so-called template files to read data from. Exceptions
        > could occur when a template file is missing, has invalid data, etc. That
        > should never happen but IF it happens, execution cannot continue.
        > I was really wondering if catching and rethrowing an exception is very bad
        > performance-wise (I guess it is, since catching costs performance) so I
        > don't want to go that way.
        > I've been thinking of returning some value myself, for example return null
        > instead of an object and check if an object is null ---> something must
        > have gone wrong. But I'd have to use THAT throughout class D to A, and I
        > was wondering if there was just one 'fast' way.
        > Isn't it possible to use events for this? If an exception in D happens, an
        > event is raised in A...?
        >
        > Thanks,
        >
        > Razzie
        >
        > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
        > in message news:OcinejnwEH A.4040@TK2MSFTN GP11.phx.gbl...[color=green]
        >> Razzie,
        >>
        >> If you want to invalidate the classes when an exception occurs, then
        >> you will have to use the try/catch block and set a flag indicating that
        >> the class can not be used. You would then check that flag for every
        >> operation on that class.
        >>
        >> I would say that this is a bit overkill, plus, you are not designing
        >> your classes to be resilient enough in the face of these exceptions.
        >>
        >> Also, are you throwing exceptions as a result of say, errors in logic?
        >> If so, you might want to reconsider using exceptions (is the error in
        >> logic truly an exceptional case) and use some sort of return value,
        >>
        >> Hope this helps.
        >>
        >>
        >> --
        >> - Nicholas Paldino [.NET/C# MVP]
        >> - mvp@spam.guard. caspershouse.co m
        >>
        >> "Razzie" <razzie@quickne t.nl> wrote in message
        >> news:uoKDrenwEH A.3724@TK2MSFTN GP10.phx.gbl...[color=darkred]
        >>> Hi all,
        >>>
        >>> The title of this post may sound a bit weird, but I was wondering about
        >>> the following nonetheless.
        >>>
        >>> I have a class libray containing, say, 4 classes: A, B, C, D. Class A
        >>> somehow has a reference to B, B has a reference to C, and C to D.
        >>> If an exception happens in class D, I would like class A to get a
        >>> notification of this (all execution on classes B to D should be
        >>> terminated). I am wondering how to do this. The following seems like a
        >>> bad idea:
        >>>
        >>> class D {
        >>> {
        >>> try(...)
        >>> catch(SomeExcep tion ex)
        >>> throw new SomeException(" Error in D");
        >>> }
        >>>
        >>> class C
        >>> {
        >>> D d = new D();
        >>> try
        >>> { d.doSomething() ; }
        >>> catch(SomeExcep tion ex)
        >>> { throw new SomeException(e x.Message); }
        >>> }
        >>>
        >>> and catch THAT exception in B, and throw it to A. I think you get the
        >>> idea :) I'm quite sure this is a bad idea. But how should I structure it
        >>> then? No error handling at all in classes B to D is even worse. I hope
        >>> someone can give me a good idea.
        >>>
        >>> Thanks!
        >>>
        >>> Razzie
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Hans Kesting

          #5
          Re: Exceptions throughout multiple classes

          Razzie wrote:[color=blue]
          > Hi all,
          >
          > The title of this post may sound a bit weird, but I was wondering
          > about the following nonetheless.
          >
          > I have a class libray containing, say, 4 classes: A, B, C, D. Class A
          > somehow has a reference to B, B has a reference to C, and C to D.
          > If an exception happens in class D, I would like class A to get a
          > notification of this (all execution on classes B to D should be
          > terminated). I am wondering how to do this. The following seems like
          > a bad idea:
          > class D {
          > {
          > try(...)
          > catch(SomeExcep tion ex)
          > throw new SomeException(" Error in D");
          > }
          >
          > class C
          > {
          > D d = new D();
          > try
          > { d.doSomething() ; }
          > catch(SomeExcep tion ex)
          > { throw new SomeException(e x.Message); }
          > }
          >
          > and catch THAT exception in B, and throw it to A. I think you get the
          > idea :) I'm quite sure this is a bad idea. But how should I structure
          > it then? No error handling at all in classes B to D is even worse. I
          > hope someone can give me a good idea.
          >
          > Thanks!
          >
          > Razzie[/color]

          If you call that method in D through C, B and A (that is, externally you
          only work with A), then if an exception is thrown in D (or C or B)
          you can catch it in A without a catch-and-rethrow in the intermediate
          layers.

          Hans Kesting


          Comment

          • Razzie

            #6
            Re: Exceptions throughout multiple classes

            Hehe, because I don't know how..

            Class A will be running continuously in a windows service. Depending on
            certain data it will create a new instance of B, and B uses C, and C uses D.
            How do I 'break' this chain when an error occurs in D? Maybe I'm looking way
            too difficult at this situation, but I don't see it. The only way I can see
            to tell A that something went wrong, is either rethrow every exception
            through the chain and catch it in A (which I don't want) or use specific
            return values? But I was hoping there would be an easy way. If there isn't,
            please tell me, and I'll most likely use specific return values :)

            Thanks,

            Razzie

            "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
            message news:eQO6QLowEH A.1984@TK2MSFTN GP14.phx.gbl...[color=blue]
            > Razzie,
            >
            > You could use events, but that seems like a lot of work as well.
            >
            > If D uses C and C uses B and B uses A, why not have the process that
            > makes calls on D just end? I mean, why not just ditch that process and
            > then start over, discarding the chain?
            >
            >
            > --
            > - Nicholas Paldino [.NET/C# MVP]
            > - mvp@spam.guard. caspershouse.co m
            >
            > "Razzie" <razzie@quickne t.nl> wrote in message
            > news:eGIl65nwEH A.3808@TK2MSFTN GP15.phx.gbl...[color=green]
            >> Hello Nicholas,
            >>
            >> Some classes use so-called template files to read data from. Exceptions
            >> could occur when a template file is missing, has invalid data, etc. That
            >> should never happen but IF it happens, execution cannot continue.
            >> I was really wondering if catching and rethrowing an exception is very
            >> bad performance-wise (I guess it is, since catching costs performance) so
            >> I don't want to go that way.
            >> I've been thinking of returning some value myself, for example return
            >> null instead of an object and check if an object is null ---> something
            >> must have gone wrong. But I'd have to use THAT throughout class D to A,
            >> and I was wondering if there was just one 'fast' way.
            >> Isn't it possible to use events for this? If an exception in D happens,
            >> an event is raised in A...?
            >>
            >> Thanks,
            >>
            >> Razzie
            >>
            >> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
            >> in message news:OcinejnwEH A.4040@TK2MSFTN GP11.phx.gbl...[color=darkred]
            >>> Razzie,
            >>>
            >>> If you want to invalidate the classes when an exception occurs, then
            >>> you will have to use the try/catch block and set a flag indicating that
            >>> the class can not be used. You would then check that flag for every
            >>> operation on that class.
            >>>
            >>> I would say that this is a bit overkill, plus, you are not designing
            >>> your classes to be resilient enough in the face of these exceptions.
            >>>
            >>> Also, are you throwing exceptions as a result of say, errors in
            >>> logic? If so, you might want to reconsider using exceptions (is the
            >>> error in logic truly an exceptional case) and use some sort of return
            >>> value,
            >>>
            >>> Hope this helps.
            >>>
            >>>
            >>> --
            >>> - Nicholas Paldino [.NET/C# MVP]
            >>> - mvp@spam.guard. caspershouse.co m
            >>>
            >>> "Razzie" <razzie@quickne t.nl> wrote in message
            >>> news:uoKDrenwEH A.3724@TK2MSFTN GP10.phx.gbl...
            >>>> Hi all,
            >>>>
            >>>> The title of this post may sound a bit weird, but I was wondering about
            >>>> the following nonetheless.
            >>>>
            >>>> I have a class libray containing, say, 4 classes: A, B, C, D. Class A
            >>>> somehow has a reference to B, B has a reference to C, and C to D.
            >>>> If an exception happens in class D, I would like class A to get a
            >>>> notification of this (all execution on classes B to D should be
            >>>> terminated). I am wondering how to do this. The following seems like a
            >>>> bad idea:
            >>>>
            >>>> class D {
            >>>> {
            >>>> try(...)
            >>>> catch(SomeExcep tion ex)
            >>>> throw new SomeException(" Error in D");
            >>>> }
            >>>>
            >>>> class C
            >>>> {
            >>>> D d = new D();
            >>>> try
            >>>> { d.doSomething() ; }
            >>>> catch(SomeExcep tion ex)
            >>>> { throw new SomeException(e x.Message); }
            >>>> }
            >>>>
            >>>> and catch THAT exception in B, and throw it to A. I think you get the
            >>>> idea :) I'm quite sure this is a bad idea. But how should I structure
            >>>> it then? No error handling at all in classes B to D is even worse. I
            >>>> hope someone can give me a good idea.
            >>>>
            >>>> Thanks!
            >>>>
            >>>> Razzie
            >>>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Razzie

              #7
              Re: Exceptions throughout multiple classes

              Hmm I guess I could do that... my first thought was that would be a little
              bit 'dirty' to do, but when I think of it, I guess it's not that bad...
              unless anyone thinks otherwise. thanks.

              "Hans Kesting" <news.2.hansdk@ spamgourmet.com > wrote in message
              news:u$Q15NowEH A.3724@TK2MSFTN GP10.phx.gbl...[color=blue]
              > Razzie wrote:[color=green]
              >> Hi all,
              >>
              >> The title of this post may sound a bit weird, but I was wondering
              >> about the following nonetheless.
              >>
              >> I have a class libray containing, say, 4 classes: A, B, C, D. Class A
              >> somehow has a reference to B, B has a reference to C, and C to D.
              >> If an exception happens in class D, I would like class A to get a
              >> notification of this (all execution on classes B to D should be
              >> terminated). I am wondering how to do this. The following seems like
              >> a bad idea:
              >> class D {
              >> {
              >> try(...)
              >> catch(SomeExcep tion ex)
              >> throw new SomeException(" Error in D");
              >> }
              >>
              >> class C
              >> {
              >> D d = new D();
              >> try
              >> { d.doSomething() ; }
              >> catch(SomeExcep tion ex)
              >> { throw new SomeException(e x.Message); }
              >> }
              >>
              >> and catch THAT exception in B, and throw it to A. I think you get the
              >> idea :) I'm quite sure this is a bad idea. But how should I structure
              >> it then? No error handling at all in classes B to D is even worse. I
              >> hope someone can give me a good idea.
              >>
              >> Thanks!
              >>
              >> Razzie[/color]
              >
              > If you call that method in D through C, B and A (that is, externally you
              > only work with A), then if an exception is thrown in D (or C or B)
              > you can catch it in A without a catch-and-rethrow in the intermediate
              > layers.
              >
              > Hans Kesting
              >
              >[/color]


              Comment

              • Nicholas Paldino [.NET/C# MVP]

                #8
                Re: Exceptions throughout multiple classes

                Razzie,

                Ok, if you are in a service, then you don't have just one method that is
                running continuously, right? Rather, you have some sort of event (time, a
                message coming in, etc, etc) which then triggers code to be run. At that
                point, you should create A, and then the rest of the chain.

                Then, wrap the whole event handler in a try/catch block, and don't
                bother catching events in the other classes. If an exception occurs, log it
                somewhere. You shouldn't have to worry otherwise, since you will create a
                new instance of A each time the event occurs.

                Also, I would say if you are doing something strictly on a schedule, to
                not use a service, as you are just wasting processing cycles waiting.
                Rather, use a scheduled task in windows, and create your application as an
                EXE that is run at certain times.

                Hope this helps.


                --
                - Nicholas Paldino [.NET/C# MVP]
                - mvp@spam.guard. caspershouse.co m


                "Razzie" <razzie@quickne t.nl> wrote in message
                news:%2377MXcow EHA.392@TK2MSFT NGP12.phx.gbl.. .[color=blue]
                > Hehe, because I don't know how..
                >
                > Class A will be running continuously in a windows service. Depending on
                > certain data it will create a new instance of B, and B uses C, and C uses
                > D. How do I 'break' this chain when an error occurs in D? Maybe I'm
                > looking way too difficult at this situation, but I don't see it. The only
                > way I can see to tell A that something went wrong, is either rethrow every
                > exception through the chain and catch it in A (which I don't want) or use
                > specific return values? But I was hoping there would be an easy way. If
                > there isn't, please tell me, and I'll most likely use specific return
                > values :)
                >
                > Thanks,
                >
                > Razzie
                >
                > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
                > in message news:eQO6QLowEH A.1984@TK2MSFTN GP14.phx.gbl...[color=green]
                >> Razzie,
                >>
                >> You could use events, but that seems like a lot of work as well.
                >>
                >> If D uses C and C uses B and B uses A, why not have the process that
                >> makes calls on D just end? I mean, why not just ditch that process and
                >> then start over, discarding the chain?
                >>
                >>
                >> --
                >> - Nicholas Paldino [.NET/C# MVP]
                >> - mvp@spam.guard. caspershouse.co m
                >>
                >> "Razzie" <razzie@quickne t.nl> wrote in message
                >> news:eGIl65nwEH A.3808@TK2MSFTN GP15.phx.gbl...[color=darkred]
                >>> Hello Nicholas,
                >>>
                >>> Some classes use so-called template files to read data from. Exceptions
                >>> could occur when a template file is missing, has invalid data, etc. That
                >>> should never happen but IF it happens, execution cannot continue.
                >>> I was really wondering if catching and rethrowing an exception is very
                >>> bad performance-wise (I guess it is, since catching costs performance)
                >>> so I don't want to go that way.
                >>> I've been thinking of returning some value myself, for example return
                >>> null instead of an object and check if an object is null ---> something
                >>> must have gone wrong. But I'd have to use THAT throughout class D to A,
                >>> and I was wondering if there was just one 'fast' way.
                >>> Isn't it possible to use events for this? If an exception in D happens,
                >>> an event is raised in A...?
                >>>
                >>> Thanks,
                >>>
                >>> Razzie
                >>>
                >>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
                >>> in message news:OcinejnwEH A.4040@TK2MSFTN GP11.phx.gbl...
                >>>> Razzie,
                >>>>
                >>>> If you want to invalidate the classes when an exception occurs, then
                >>>> you will have to use the try/catch block and set a flag indicating that
                >>>> the class can not be used. You would then check that flag for every
                >>>> operation on that class.
                >>>>
                >>>> I would say that this is a bit overkill, plus, you are not designing
                >>>> your classes to be resilient enough in the face of these exceptions.
                >>>>
                >>>> Also, are you throwing exceptions as a result of say, errors in
                >>>> logic? If so, you might want to reconsider using exceptions (is the
                >>>> error in logic truly an exceptional case) and use some sort of return
                >>>> value,
                >>>>
                >>>> Hope this helps.
                >>>>
                >>>>
                >>>> --
                >>>> - Nicholas Paldino [.NET/C# MVP]
                >>>> - mvp@spam.guard. caspershouse.co m
                >>>>
                >>>> "Razzie" <razzie@quickne t.nl> wrote in message
                >>>> news:uoKDrenwEH A.3724@TK2MSFTN GP10.phx.gbl...
                >>>>> Hi all,
                >>>>>
                >>>>> The title of this post may sound a bit weird, but I was wondering
                >>>>> about the following nonetheless.
                >>>>>
                >>>>> I have a class libray containing, say, 4 classes: A, B, C, D. Class A
                >>>>> somehow has a reference to B, B has a reference to C, and C to D.
                >>>>> If an exception happens in class D, I would like class A to get a
                >>>>> notification of this (all execution on classes B to D should be
                >>>>> terminated). I am wondering how to do this. The following seems like a
                >>>>> bad idea:
                >>>>>
                >>>>> class D {
                >>>>> {
                >>>>> try(...)
                >>>>> catch(SomeExcep tion ex)
                >>>>> throw new SomeException(" Error in D");
                >>>>> }
                >>>>>
                >>>>> class C
                >>>>> {
                >>>>> D d = new D();
                >>>>> try
                >>>>> { d.doSomething() ; }
                >>>>> catch(SomeExcep tion ex)
                >>>>> { throw new SomeException(e x.Message); }
                >>>>> }
                >>>>>
                >>>>> and catch THAT exception in B, and throw it to A. I think you get the
                >>>>> idea :) I'm quite sure this is a bad idea. But how should I structure
                >>>>> it then? No error handling at all in classes B to D is even worse. I
                >>>>> hope someone can give me a good idea.
                >>>>>
                >>>>> Thanks!
                >>>>>
                >>>>> Razzie
                >>>>>
                >>>>
                >>>>
                >>>
                >>>[/color]
                >>
                >>[/color]
                >
                >[/color]


                Comment

                • Razzie

                  #9
                  Re: Exceptions throughout multiple classes

                  Allright thanks for the excellent help Nicholas. And thank you too Hans
                  Kesting.

                  As for the service - yeah data is coming in, but at very random intervals :)

                  "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                  message news:etR7BiowEH A.3624@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > Razzie,
                  >
                  > Ok, if you are in a service, then you don't have just one method that
                  > is running continuously, right? Rather, you have some sort of event
                  > (time, a message coming in, etc, etc) which then triggers code to be run.
                  > At that point, you should create A, and then the rest of the chain.
                  >
                  > Then, wrap the whole event handler in a try/catch block, and don't
                  > bother catching events in the other classes. If an exception occurs, log
                  > it somewhere. You shouldn't have to worry otherwise, since you will
                  > create a new instance of A each time the event occurs.
                  >
                  > Also, I would say if you are doing something strictly on a schedule, to
                  > not use a service, as you are just wasting processing cycles waiting.
                  > Rather, use a scheduled task in windows, and create your application as an
                  > EXE that is run at certain times.
                  >
                  > Hope this helps.
                  >
                  >
                  > --
                  > - Nicholas Paldino [.NET/C# MVP]
                  > - mvp@spam.guard. caspershouse.co m
                  >
                  >
                  > "Razzie" <razzie@quickne t.nl> wrote in message
                  > news:%2377MXcow EHA.392@TK2MSFT NGP12.phx.gbl.. .[color=green]
                  >> Hehe, because I don't know how..
                  >>
                  >> Class A will be running continuously in a windows service. Depending on
                  >> certain data it will create a new instance of B, and B uses C, and C uses
                  >> D. How do I 'break' this chain when an error occurs in D? Maybe I'm
                  >> looking way too difficult at this situation, but I don't see it. The only
                  >> way I can see to tell A that something went wrong, is either rethrow
                  >> every exception through the chain and catch it in A (which I don't want)
                  >> or use specific return values? But I was hoping there would be an easy
                  >> way. If there isn't, please tell me, and I'll most likely use specific
                  >> return values :)
                  >>
                  >> Thanks,
                  >>
                  >> Razzie
                  >>
                  >> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
                  >> in message news:eQO6QLowEH A.1984@TK2MSFTN GP14.phx.gbl...[color=darkred]
                  >>> Razzie,
                  >>>
                  >>> You could use events, but that seems like a lot of work as well.
                  >>>
                  >>> If D uses C and C uses B and B uses A, why not have the process that
                  >>> makes calls on D just end? I mean, why not just ditch that process and
                  >>> then start over, discarding the chain?
                  >>>
                  >>>
                  >>> --
                  >>> - Nicholas Paldino [.NET/C# MVP]
                  >>> - mvp@spam.guard. caspershouse.co m
                  >>>
                  >>> "Razzie" <razzie@quickne t.nl> wrote in message
                  >>> news:eGIl65nwEH A.3808@TK2MSFTN GP15.phx.gbl...
                  >>>> Hello Nicholas,
                  >>>>
                  >>>> Some classes use so-called template files to read data from. Exceptions
                  >>>> could occur when a template file is missing, has invalid data, etc.
                  >>>> That should never happen but IF it happens, execution cannot continue.
                  >>>> I was really wondering if catching and rethrowing an exception is very
                  >>>> bad performance-wise (I guess it is, since catching costs performance)
                  >>>> so I don't want to go that way.
                  >>>> I've been thinking of returning some value myself, for example return
                  >>>> null instead of an object and check if an object is null ---> something
                  >>>> must have gone wrong. But I'd have to use THAT throughout class D to A,
                  >>>> and I was wondering if there was just one 'fast' way.
                  >>>> Isn't it possible to use events for this? If an exception in D happens,
                  >>>> an event is raised in A...?
                  >>>>
                  >>>> Thanks,
                  >>>>
                  >>>> Razzie
                  >>>>
                  >>>> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
                  >>>> wrote in message news:OcinejnwEH A.4040@TK2MSFTN GP11.phx.gbl...
                  >>>>> Razzie,
                  >>>>>
                  >>>>> If you want to invalidate the classes when an exception occurs,
                  >>>>> then you will have to use the try/catch block and set a flag
                  >>>>> indicating that the class can not be used. You would then check that
                  >>>>> flag for every operation on that class.
                  >>>>>
                  >>>>> I would say that this is a bit overkill, plus, you are not
                  >>>>> designing your classes to be resilient enough in the face of these
                  >>>>> exceptions.
                  >>>>>
                  >>>>> Also, are you throwing exceptions as a result of say, errors in
                  >>>>> logic? If so, you might want to reconsider using exceptions (is the
                  >>>>> error in logic truly an exceptional case) and use some sort of return
                  >>>>> value,
                  >>>>>
                  >>>>> Hope this helps.
                  >>>>>
                  >>>>>
                  >>>>> --
                  >>>>> - Nicholas Paldino [.NET/C# MVP]
                  >>>>> - mvp@spam.guard. caspershouse.co m
                  >>>>>
                  >>>>> "Razzie" <razzie@quickne t.nl> wrote in message
                  >>>>> news:uoKDrenwEH A.3724@TK2MSFTN GP10.phx.gbl...
                  >>>>>> Hi all,
                  >>>>>>
                  >>>>>> The title of this post may sound a bit weird, but I was wondering
                  >>>>>> about the following nonetheless.
                  >>>>>>
                  >>>>>> I have a class libray containing, say, 4 classes: A, B, C, D. Class A
                  >>>>>> somehow has a reference to B, B has a reference to C, and C to D.
                  >>>>>> If an exception happens in class D, I would like class A to get a
                  >>>>>> notification of this (all execution on classes B to D should be
                  >>>>>> terminated). I am wondering how to do this. The following seems like
                  >>>>>> a bad idea:
                  >>>>>>
                  >>>>>> class D {
                  >>>>>> {
                  >>>>>> try(...)
                  >>>>>> catch(SomeExcep tion ex)
                  >>>>>> throw new SomeException(" Error in D");
                  >>>>>> }
                  >>>>>>
                  >>>>>> class C
                  >>>>>> {
                  >>>>>> D d = new D();
                  >>>>>> try
                  >>>>>> { d.doSomething() ; }
                  >>>>>> catch(SomeExcep tion ex)
                  >>>>>> { throw new SomeException(e x.Message); }
                  >>>>>> }
                  >>>>>>
                  >>>>>> and catch THAT exception in B, and throw it to A. I think you get the
                  >>>>>> idea :) I'm quite sure this is a bad idea. But how should I structure
                  >>>>>> it then? No error handling at all in classes B to D is even worse. I
                  >>>>>> hope someone can give me a good idea.
                  >>>>>>
                  >>>>>> Thanks!
                  >>>>>>
                  >>>>>> Razzie
                  >>>>>>
                  >>>>>
                  >>>>>
                  >>>>
                  >>>>
                  >>>
                  >>>[/color]
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • Bonj

                    #10
                    Re: Exceptions throughout multiple classes

                    Use a return value of bool for every function to indicate success or
                    failure, then just propogate this up. You can use out or ref parameters for
                    the actual return values. I've used this to much success - if the function
                    fails, it simply puts null in the out parameter, but the return value
                    naturally lends itself to an if statement which separates what happens in an
                    error to what happens if successful, in the front end code - so you're not
                    going to mind that it's null because if it is, you'll be in the code block
                    that writes "There was an error". Don't make the mistake of putting 'return
                    true' in the finally though - put it at the end of the try.
                    e.g.
                    class D
                    {
                    static bool DoTheActualWork (out string retval)
                    {
                    try
                    {
                    ...do the work, that assigns to retval
                    return true;
                    }
                    catch(Exception ex) {retval = null; return false;}
                    }
                    }
                    class C { static bool GetRawData(out string retval) {return
                    D.DoTheActualWo rk(out retval); }
                    class B { static bool GetData(out string retval) {return C.GetRawData(ou t
                    retval); }
                    class A
                    {
                    static void Main()
                    {
                    string thedata;
                    if(B.GetData(ou t thedata)) Console.WriteLi ne("The data is {0}",
                    thedata);
                    else Console.WriteLi ne("There was an error!");
                    }
                    }

                    Another cunning use of finally blocks is when you aren't necessarily worried
                    about an exception, but want to return from a function the return value of a
                    method call on an object, but that object needs cleaning up. You then don't
                    need to assign a variable.
                    e.g.
                    objectdata getdata(someobj ect theobj)
                    {
                    try{return theobj.getheobj ectdatathatwont fail();}
                    finally{theobj. cleanitup();}
                    }


                    "Razzie" <razzie@quickne t.nl> wrote in message
                    news:uoKDrenwEH A.3724@TK2MSFTN GP10.phx.gbl...[color=blue]
                    > Hi all,
                    >
                    > The title of this post may sound a bit weird, but I was wondering about
                    > the following nonetheless.
                    >
                    > I have a class libray containing, say, 4 classes: A, B, C, D. Class A
                    > somehow has a reference to B, B has a reference to C, and C to D.
                    > If an exception happens in class D, I would like class A to get a
                    > notification of this (all execution on classes B to D should be
                    > terminated). I am wondering how to do this. The following seems like a bad
                    > idea:
                    >
                    > class D {
                    > {
                    > try(...)
                    > catch(SomeExcep tion ex)
                    > throw new SomeException(" Error in D");
                    > }
                    >
                    > class C
                    > {
                    > D d = new D();
                    > try
                    > { d.doSomething() ; }
                    > catch(SomeExcep tion ex)
                    > { throw new SomeException(e x.Message); }
                    > }
                    >
                    > and catch THAT exception in B, and throw it to A. I think you get the idea
                    > :) I'm quite sure this is a bad idea. But how should I structure it then?
                    > No error handling at all in classes B to D is even worse. I hope someone
                    > can give me a good idea.
                    >
                    > Thanks!
                    >
                    > Razzie
                    >[/color]


                    Comment

                    • Razzie

                      #11
                      Re: Exceptions throughout multiple classes

                      Thanks for the suggestion. I'll give it a shot tomorrow!

                      "Bonj" <benjtaylor at hotpop d0t com> wrote in message
                      news:eVPUYQrwEH A.3292@TK2MSFTN GP15.phx.gbl...[color=blue]
                      > Use a return value of bool for every function to indicate success or
                      > failure, then just propogate this up. You can use out or ref parameters
                      > for the actual return values. I've used this to much success - if the
                      > function fails, it simply puts null in the out parameter, but the return
                      > value naturally lends itself to an if statement which separates what
                      > happens in an error to what happens if successful, in the front end code -
                      > so you're not going to mind that it's null because if it is, you'll be in
                      > the code block that writes "There was an error". Don't make the mistake of
                      > putting 'return true' in the finally though - put it at the end of the
                      > try.
                      > e.g.
                      > class D
                      > {
                      > static bool DoTheActualWork (out string retval)
                      > {
                      > try
                      > {
                      > ...do the work, that assigns to retval
                      > return true;
                      > }
                      > catch(Exception ex) {retval = null; return false;}
                      > }
                      > }
                      > class C { static bool GetRawData(out string retval) {return
                      > D.DoTheActualWo rk(out retval); }
                      > class B { static bool GetData(out string retval) {return C.GetRawData(ou t
                      > retval); }
                      > class A
                      > {
                      > static void Main()
                      > {
                      > string thedata;
                      > if(B.GetData(ou t thedata)) Console.WriteLi ne("The data is {0}",
                      > thedata);
                      > else Console.WriteLi ne("There was an error!");
                      > }
                      > }
                      >
                      > Another cunning use of finally blocks is when you aren't necessarily
                      > worried about an exception, but want to return from a function the return
                      > value of a method call on an object, but that object needs cleaning up.
                      > You then don't need to assign a variable.
                      > e.g.
                      > objectdata getdata(someobj ect theobj)
                      > {
                      > try{return theobj.getheobj ectdatathatwont fail();}
                      > finally{theobj. cleanitup();}
                      > }
                      >
                      >
                      > "Razzie" <razzie@quickne t.nl> wrote in message
                      > news:uoKDrenwEH A.3724@TK2MSFTN GP10.phx.gbl...[color=green]
                      >> Hi all,
                      >>
                      >> The title of this post may sound a bit weird, but I was wondering about
                      >> the following nonetheless.
                      >>
                      >> I have a class libray containing, say, 4 classes: A, B, C, D. Class A
                      >> somehow has a reference to B, B has a reference to C, and C to D.
                      >> If an exception happens in class D, I would like class A to get a
                      >> notification of this (all execution on classes B to D should be
                      >> terminated). I am wondering how to do this. The following seems like a
                      >> bad idea:
                      >>
                      >> class D {
                      >> {
                      >> try(...)
                      >> catch(SomeExcep tion ex)
                      >> throw new SomeException(" Error in D");
                      >> }
                      >>
                      >> class C
                      >> {
                      >> D d = new D();
                      >> try
                      >> { d.doSomething() ; }
                      >> catch(SomeExcep tion ex)
                      >> { throw new SomeException(e x.Message); }
                      >> }
                      >>
                      >> and catch THAT exception in B, and throw it to A. I think you get the
                      >> idea :) I'm quite sure this is a bad idea. But how should I structure it
                      >> then? No error handling at all in classes B to D is even worse. I hope
                      >> someone can give me a good idea.
                      >>
                      >> Thanks!
                      >>
                      >> Razzie
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      Working...