我有一個構造的方法來創建一個對象
public Track(string path)
{
if (!File.Exists(path))
throw new FileNotFoundException("File not found", path);
if (!IsAudioFile(path))
throw new Exception("Illegal Audio Format");
_path = path;
_id = Guid.NewGuid();
_rate = 0;
_length = GetTrackLength(path);
TagLib.File file = TagLib.File.Create(path);
if (!file.Tag.IsEmpty)
{
try
{
_artist = file.Tag.Artists[0];
}
catch (Exception e)
{
_artist = "";
}
_title = file.Tag.Title;
try
{
_genre = file.Tag.Genres[0].ToGenre();
}
catch (Exception e)
{
_genre = Genre.NoGenre;
}
}
else
{
_artist = "Unknown";
_title = "Unknown";
_genre = Genre.NoGenre;
}
}
我應該拋出一個異常,或者我應該選擇創建對象的另一種方式? 例如:
Track track = new Track(path);
track = Track.GetInstance();
你認爲我做的一切正確? – Sergey 2010-11-14 15:14:31
更好使用ArgumentException,我認爲 – 2010-11-14 15:15:02
@Segey:差不多。 – SLaks 2010-11-14 15:15:46