473,890 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 16 '05 #1
10 1307
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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:uo******** ******@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

Nov 16 '05 #2
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]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:Oc******** ******@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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:uo******** ******@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


Nov 16 '05 #3
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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:eG******** ******@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]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:Oc******** ******@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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:uo******** ******@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



Nov 16 '05 #4
Razzie wrote:
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


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
Nov 16 '05 #5
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]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:eQ******** ******@TK2MSFTN GP14.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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:eG******** ******@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]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:Oc******** ******@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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:uo******** ******@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



Nov 16 '05 #6
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" <ne***********@ spamgourmet.com > wrote in message
news:u$******** ******@TK2MSFTN GP10.phx.gbl...
Razzie wrote:
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


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

Nov 16 '05 #7
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]
- mv*@spam.guard. caspershouse.co m
"Razzie" <ra****@quickne t.nl> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
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]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eQ******** ******@TK2MSFTN GP14.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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:eG******** ******@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]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:Oc******** ******@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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:uo******** ******@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
>



Nov 16 '05 #8
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]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:et******** ******@TK2MSFTN GP09.phx.gbl...
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]
- mv*@spam.guard. caspershouse.co m
"Razzie" <ra****@quickne t.nl> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
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]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eQ******** ******@TK2MSFTN GP14.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]
- mv*@spam.guard. caspershouse.co m

"Razzie" <ra****@quickne t.nl> wrote in message
news:eG******** ******@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]" <mv*@spam.guard .caspershouse.c om>
wrote in message news:Oc******** ******@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]
> - mv*@spam.guard. caspershouse.co m
>
> "Razzie" <ra****@quickne t.nl> wrote in message
> news:uo******** ******@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
>>
>
>



Nov 16 '05 #9
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" <ra****@quickne t.nl> wrote in message
news:uo******** ******@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

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
2930
by: OvErboRed | last post by:
I just read a whole bunch of threads on microsoft.public.dotnet.* regarding checked exceptions (the longest-running of which seems to be <cJQQ9.4419 $j94.834878@news02.tsnz.net>. My personal belief is that checked exceptions should be required in .NET. I find that many others share the same views as I do. It is extremely frustrating to have to work around this with hacks like Abstract ADO.NET and CLRxLint (which still don't solve the...
14
2310
by: Noah Roberts | last post by:
I am a bit confused about how inheritance works with regard to exceptions apparently. class ParentEx : public std::exception { }; class SubEx : public ParentEx,std::runtime_error { };
822
29947
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical real-time applications. The majority of expert/people recommend Ada for safety critical real-time applications. I've many years of experience in C/C++ (and Delphi) but no Ada knowledge. May I ask if it is too difficult to move from C/C++ to Ada?...
2
1697
by: Ramza Brown | last post by:
This mysql_error doesnt give me an error below, but when I run it at the global scope it does give me errors. I went ahead and checked for mysql_error(), but normally I dont have to, why? public function runQuery() { try { /// Use mysql_error() to check for errors if (!empty($this->build_query)) { $this->sql_result = mysql_query($this->build_query);
8
2265
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an exception should be thrown only in exceptional situations. It turned out that each of my colleagues had their own interpretation about what an "exceptional situation" may actually be. First of all, myself I’m against using exceptions extensively,...
4
1859
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and it has now, after 15k lines of code resulted in a royal mess! It's my hope to ask some specific questions with scenario examples and that some of you might offer a little guidance or general suggestions. 1) string...
2
2973
by: Zytan | last post by:
I know that WebRequest.GetResponse can throw WebException from internet tutorials. However in the MSDN docs: http://msdn2.microsoft.com/en-us/library/system.net.webrequest.getresponse.aspx It only lists NotImplementedException in the Exceptions section. (Note that it does mention WebException in the Remarks section, but who knows if this is always the case for such classes, and thus perhaps they can't be trusted to always list these, and...
0
6511
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like NullPointerException or ArrayIndexOutOfBoundsException. All of these extend the basic class Exception. In general, you can sort Exceptions into two groups: Checked and unchecked Exceptions. Checked Exceptions are checked by the compiler at compilation time. Most...
4
3635
by: Slaunger | last post by:
Hi there, I am a newcomer to Pyhton coming from Java working on a relatively large Pyhton project with several packages and modules. To improve exception handling I would like to introduce some user-defined exceptions to distinguish between exceptions raised in self-written code as compared to std libray modules used there-in. Say, for instance I would like to define a MyParseError exception to indicate that something has gone wrong...
0
9976
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9815
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7160
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.