この質問は私がやりたいことに近いですが、完全にはありません。
次のコードを簡略化する方法はありますか?
private bool ValidDirectory(string directory)
{
if (!Directory.Exists(directory))
{
if (MessageBox.Show(directory + " does not exist. Do you wish to create it?", this.Text)
== DialogResult.OK)
{
try
{
Directory.CreateDirectory(directory);
return true;
}
catch (IOException ex)
{
lblBpsError.Text = ex.Message;
}
catch (UnauthorizedAccessException ex)
{
lblBpsError.Text = ex.Message;
}
catch (PathTooLongException ex)
{
lblBpsError.Text = ex.Message;
}
catch (DirectoryNotFoundException ex)
{
lblBpsError.Text = ex.Message;
}
catch (NotSupportedException ex)
{
lblBpsError.Text = ex.Message;
}
}
}
return false;
}
無駄に思えます。後でエラーをユーザーに報告する方法を変更したい場合、またはこれらのエラーをログに記録したい場合などは、5つの異なるキャッチブロックを変更する必要があります。私は何かが足りないのですか、それともこれはコードの再利用に対して露骨に反対ですか?
私は(あまりにも)怠惰になろうとしているだけですか?