
Exceptions - Throwing Between My Components
First of all, make sure your exception inherits from the closest match you
can find. They programmer may not know what "NegativesNotAllowedException"
means, but if you base it on "ArgumentException" he has a pretty good guess.
If you base it on "ArgumentOutOfRange", he has an even better idea. This is
especially important because he may not even know your exception exists, but
he is trapping the common types.
Secondly, use the "Catch and Rethrow" method. This way the exception appears
to be coming from your class and not one you are using. As a programmer, if
I see an exception with a stack trace several layers deeper than your class,
I think you screwed up. If I see an exception coming directly from your
class, I think I screwed up.
Try
OpenFIle( FileName)
Catch err as Exception
//Version 1, rethrow the error
Throw err
//Version 2, new exception
Throw New MyException("You gave me a bad filename", err)
End Try
Notice that is the second version, you are passing the underlying exception.
You do this so they can see your custom error message as well as the
underlying cause. This is called an "InnerException". You can create a chain
of inner exceptions as long as you like.
--
Jonathan Allen
Quote:
> Hope this is my last problem with exceptions in C#.
> I am sure sooner or later existing exception will not satisfy all my
needs.
> So I will have to develop my custom exception classes.
> Problems may arise if there are several different modules in my
application
> throwing exceptions to each other.
> Are there any known approaches to this problem worth recommendation?
> Thanks in advance for any tips/pointers.
> Tom Jastrzebski