tags:

views:

152

answers:

6

Hi there,

I'm writing yet another ActiveRecord implementation for a company that is less scared of my code than they are the designation "Release Candidate" on CastleProject's implementation. Anyway, I'm using Attributes on each property in the base class to map them to the returning DataSet's columns:

[ResultColumnAttribute("CUST_FIRST_NAME")]
public string FirstName
        {
            get { return _columnName; }
            set { _columnName = value; }
        }

so that when I instantiate the class from a DataSet, I assign that property value the column's value. What exception should I throw when a column is mapped with an attribute, but doesn't show up in the DataSet? I don't want to go and write a custom one (lazy), and I think Application.Exception is a little nondescript.

+8  A: 

This exception is localized to your domain and as such I think you would be better off writing your own InvalidMappingException.

Here is how I would write it:

[Serializable]
public class InvalidMappingException : Exception
{
    public InvalidMappingException() { }

    public InvalidMappingException(String message)
     : base(message) { }

    public InvalidMappingException
        (String message, Exception innerException)
         : base(message, innerException) { }

    protected InvalidMappingException
        (SerializationInfo info, StreamingContext context)
         : base(info, context) { }
}
Andrew Hare
+1, Custom exceptions take a few seconds to write and are invaluable for "good" exception handling.
Michael Meadows
Agreed, this is a situation where you need your own custom exception. +1
DoctaJonez
I'm surprised that no one has created a T4 template for custom exceptions.
Michael Meadows
I disagree. I see no value in this exception unless callers will explicitly use catch (InvalidMappingException) and do something specific with the fact that there was an invalid mapping.
John Saunders
@Michael Meadows: You don't need one - there is an "exception" snipppet. :)
Andrew Hare
@John Saunders: Is there really any harm in creating a custom exception? How can you know that no one will *ever* need to catch this exception? I think that in this instance a custom exception is a good thing because it is specific and causes no harm.
Andrew Hare
I like this because "InvalidMappingException" will show up in logs and will be thrown in the development environment, giving a more polished impression of my work. If only I could check these mappings at compile time... new question maybe?
Chris McCall
@Chris: Custom FxCop step :)
Jeff Yates
@Chris: What is the value of InvalidMappingException showing up in logs? Is someone going to take a different action based on the exception name? If you're just trying to count the number of times this happens, then use a custom performance counter.
John Saunders
@Andrew: the harm is in having yet another class to maintain, and yet another exception developers will need to choose from. This is especially bad in this case, since it seems that only one piece of code will ever throw the exception, and no piece of code will ever catch it.
John Saunders
@John: I see your point but I hardly think that this exception class will require much maintenance and since it is so specifically named it ought not confuse developers as they will know whether or not they need to use it.
Andrew Hare
A: 

First off, Castle Windsor is now officially version 2, no release candidate :)

I think there is only a limited set of Exceptions within the .NET framework you can really throw from your code:

  • NotImplementedException
  • InvalidOperationException
  • ArgumentException
  • ArgumentNullException

there may be more, I am hoping for other posts. If none of them fits you should write your own. With the "exc" code snippet in Visual Studio, lazyness is not really an argument.

flq
We treat the root "Exception" as an assert and permit it to be thrown.
Joshua
+1  A: 

I would write a custom exception, specific to your implementation. This is a very specific, customized situation, and a custom exception will probably fit better than anything else.

To me, in this instance, (lazy) isn't a good enough reason to avoid adding the little, tiny bit of code required to make a custom exception when it's warranted...

Reed Copsey
A: 

Agreed that you should just write your own (heck, just type exception and hit Tab and the editor does 2/3 of the work for you!), but if not, I'd probably go with ArgumentException.

Eddie Deyo
+2  A: 

I see no reason not to use InvalidOperationException. Remember that unless your callers will do something programmatic with your exception, the exception type does not matter. For instance, if they are going to catch it explicitly, or reference some property of your exception, then you need one of your own.

Otherwise, the built-in exceptions will do fine.


See How to Design Exception Hierarchies by Krzysztof Cwalina, coauthor of Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries.

John Saunders
+1 for linked references
Chris McCall
+1  A: 

Since "throw new Exception" is now officially bad style and out of fashion, I throw InvalidOperationException("descriptive message") for all errors I'm too lazy to write a custom error for.

MatthewMartin
@Matthew: where do you see that throw new Exception is out of style?
John Saunders
It's so out of style, it's everywhere...
Sekhat
FxCop throws you in jail for it ;)
womp
I think throwing a new Exception is good for spikes and so forth. But it's about as useful as "On Error Resume Next" in frameworks.
Matthew Whited
The FxCop team says it better than I can http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx and http://blogs.msdn.com/fxcop/archive/2007/01/22/faq-what-exception-should-i-throw-instead-of-the-reserved-exceptions-found-by-donotraisereservedexceptiontypes.aspx
MatthewMartin
@MatthewMartin: Those blog posts talk about not catching "Exception". They say nothing about not throwing it.
John Saunders
@Matthew Whited: On Error Resume next ignores errors. How does throwing new Ecception("message") ignore an error?
John Saunders
@John Saunders. I'll quote "Throwing a general exception type such as **System.Exception** (snip) in **a library** or Framework forces consumers to catch all exceptions, including unknown exceptions that they do not know how to handle (snip)?"
MatthewMartin