Exceptions - Throwing Between My Components 
Author Message
 Exceptions - Throwing Between My Components

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



Tue, 15 Jul 2003 02:43:18 GMT  
 Exceptions - Throwing Between My Components

Quote:
>I am sure sooner or later existing exception will not satisfy all my needs.
>So I will have to develop my custom exception classes.
>Thanks in advance for any tips/pointers.

- Make a list of all forseeable exceptions you want to throw, so
different programmers don't create similar exceptions

- Make SURE there is no existing exception that covers it

- Create the exception classes before anything else



Tue, 15 Jul 2003 03:10:46 GMT  
 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



Tue, 15 Jul 2003 03:31:46 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. how to throw exceptions from a COM component?

2. Exception handling - how to know which exceptions are thrown

3. property throws Exception in visual designer!

4. Throwing Exceptions in Constructors

5. System.Drawing.SafeNativeMethods threw an exception

6. std::list.clear() throws exceptions in .NET 2003.

7. Function that only throws an exception

8. too many controls throw exception

9. OK to throw exception from constructor?

10. __raise throws exception

11. Behavior of throw() exception specification?

12. throw overflow exception

 

 
Powered by phpBB® Forum Software