[プログラミング]環境改善 その2
もう一つ、同じ場所にgnarlさんの書かれた例外テストの静的クラスの方もすばらしく便利なので、
ThrowsNothing()を追加して、ダミーテストを可能にして利用させて頂いています。
それとは別にテストではなく、実際の関数でも戻り値が例外で戻ってくる奴が気持ち悪かったんですが、コードを流用させて頂き、boolを返すようにしてみました。
public static class Does { public static OperationHolder Func(OperationDelegate op) { return new OperationHolder(op); } public delegate void OperationDelegate(); public class OperationHolder { public OperationHolder(OperationDelegate o) { op = o; } private readonly OperationDelegate op; public bool Throw<T>() where T : Exception { try { op(); } catch (T) { return true; } //予定された例外 catch (Exception) { throw; } //やっちゃいました return false; } public bool Throw<T1, T2>() where T1 : Exception where T2 : Exception { try { op(); } catch (T1) { return true; } //予定された例外1 catch (T2) { return true; } //予定された例外2 catch (Exception) { throw; } //やっちゃいました return false; } } }
クラス名がDoesというのは何か引っかかりますが
if (Does.Func(() => str = streamreader.ReadLine()).Throw<System.IO.IOExeption>()) { return ""; }
こんな感じで書けるようになって幸せ。
これで例外のトラップも手を抜かずに済む。かも。