Exceptions throughout multiple classes

  • Thread starter Thread starter Razzie
  • Start date Start date
R

Razzie

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
 
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.
 
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 said:
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]
- (e-mail address removed)

Razzie said:
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
 
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]
- (e-mail address removed)

Razzie said:
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 said:
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]
- (e-mail address removed)

Razzie said:
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
 
Razzie said:
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

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
 
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 said:
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]
- (e-mail address removed)

Razzie said:
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 said:
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]
- (e-mail address removed)

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
 
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.
 
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]
- (e-mail address removed)


Razzie said:
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 said:
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]
- (e-mail address removed)

Razzie said:
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

in message 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]
- (e-mail address removed)

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
 
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 said:
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]
- (e-mail address removed)


Razzie said:
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 said:
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]
- (e-mail address removed)

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]" <[email protected]>
wrote in message 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]
- (e-mail address removed)

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
 
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.DoTheActualWork(out retval); }
class B { static bool GetData(out string retval) {return C.GetRawData(out
retval); }
class A
{
static void Main()
{
string thedata;
if(B.GetData(out thedata)) Console.WriteLine("The data is {0}",
thedata);
else Console.WriteLine("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(someobject theobj)
{
try{return theobj.getheobjectdatathatwontfail();}
finally{theobj.cleanitup();}
}
 
Back
Top