Re: Exceptions throughout multiple classes

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Razzie (razzie_at_quicknet.nl)
Date: 11/04/04


Date: Thu, 4 Nov 2004 16:58:10 +0100

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.com> wrote in
message news:eQO6QLowEHA.1984@TK2MSFTNGP14.phx.gbl...
> 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.com
>
> "Razzie" <razzie@quicknet.nl> wrote in message
> news:eGIl65nwEHA.3808@TK2MSFTNGP15.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.com> wrote
>> in message news:OcinejnwEHA.4040@TK2MSFTNGP11.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.com
>>>
>>> "Razzie" <razzie@quicknet.nl> wrote in message
>>> news:uoKDrenwEHA.3724@TK2MSFTNGP10.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(SomeException ex)
>>>> throw new SomeException("Error in D");
>>>> }
>>>>
>>>> class C
>>>> {
>>>> D d = new D();
>>>> try
>>>> { d.doSomething(); }
>>>> catch(SomeException ex)
>>>> { throw new SomeException(ex.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
>>>>
>>>
>>>
>>
>>
>
>



Relevant Pages

  • Re: Exceptions throughout multiple classes
    ... point, you should create A, and then the rest of the chain. ... 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 or use> specific return values? ... >> Razzie, ... error in logic truly an exceptional case) and use some sort of return ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TargetInvocationException makes no sense
    ... One can browse the chain by adding the exception variable to the Watch ... The key property is InnerException - you should ...
    (microsoft.public.dotnet.languages.vb)
  • Re: TargetInvocationException makes no sense
    ... One can browse the chain by adding the exception variable to the Watch ... The key property is InnerException - you should ...
    (microsoft.public.dotnet.languages.vb)
  • Re: TargetInvocationException makes no sense
    ... One can browse the chain by adding the exception variable to the Watch ... The key property is InnerException - you should ...
    (microsoft.public.dotnet.framework)
  • Re: TargetInvocationException makes no sense
    ... One can browse the chain by adding the exception variable to the Watch ... The key property is InnerException - you should ...
    (microsoft.public.dotnet.framework)