Correct Use of .NET Exception

Posted by destructo_gold on Stack Overflow See other posts from Stack Overflow or by destructo_gold
Published on 2010-04-04T06:42:49Z Indexed on 2010/04/04 6:53 UTC
Read the original article Hit count: 415

Filed under:
|

What is the correct exception to throw in the following instance?

If, for example, I have a class: Album with a collection of Songs:

List<Song>

And a method within Album to add a Song:

public void AddSong(Song song)
{
    songs.Add(song);
}

Should I throw an exception of a user attempts to add a song that already exists? If so, what type of exception?

I have heard the phrase: "Only use exceptions in exceptional circumstances", but I want to tell the client implementing Album exactly what has gone wrong (not just return a Boolean value).

© Stack Overflow or respective owner

Related posts about c#

Related posts about exceptions