Correct ways to throw custom exceptions &when to use inner excepti

G

Guest

Hi, I've created a custom exception class that inherits from
System.ApplicationException (implemented in a similar manner to the
recommendations made in the Exception Management Architecture Guide).

My question is: What’s the correct way to throw a custom exception in the
following scenario?

Example scenario:

Custom Exception hierarchy:
+---System.Exception
| +---Application.Exception
| | +---CustomBaseAbstractException
| | | +---UtilityEx

If client code calls a function in one of Utility's types and the parameters
are two empty strings-- Is it better to throw the custom exception like this:

Throw New UtilityEx(BAD_PARAM_EX_MESSAGE)

OR-- since ArgumentException is the specific "way" to say "I can't use these
parameters" is it better to set an instance of an argumentException to the
UtilityEx ‘s "inner exception" property?

Throw New UtilityEx(New ArgumentException),BAD_PARAM_EX_MESSAGE)

Should I only use inner exceptions when catching real exceptions or can you
also just create one for the sake of specificity even though the runtime has
not thrown one?

Thanks,
-Chris
 
F

Francois Bonin [C# MVP]

Well ... Even though you can create a given exception and set it as the
inner exception of an Exception object you are throwing, it is not the
intended way to use inner exceptions. You should use those only when
rethrowing an exception in response to catching something thrown.
And presumably, your custom exceptions provide you with more information
than something generic that the runtime may have thrown.

HTH
Cois
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top