以下の 2 つのコード ブロックを比較します。
if (args.Length != 1)
{
throw new ArgumentException();
}
if (String.IsNullOrEmpty(args[0])
{
throw new ArgumentNullException();
}
File f = new File(args[0]);
if (!f.Exists)
{
throw new FileNotFoundException();
}
// Perform tasks on the file
この:
if (args.Length != 1)
{
throw new ArgumentException();
}
else if (String.IsNullOrEmpty(args[0])
{
throw new ArgumentNullException();
}
else
{
File f = new File(args[0]);
if (!f.Exists)
{
throw new FileNotFoundException();
}
else
{
//Perform tasks on the file
}
}
(パフォーマンス/読みやすさの点で)どちらが優れていますか? アプリケーションには操作対象のファイルが必要なため、コードで例外をキャッチすることはありません。