LoginSignup
1
0

More than 3 years have passed since last update.

【C#】throw式

Last updated at Posted at 2020-07-09

★throw式が記述できる箇所は以下の3か所
1:ラムダ式、式形式メンバーの「=>」の右辺
2:Null合体演算子(??)の右辺
3:条件演算子の2,3つ目の引数
上記以外での記述はコンパイルエラーとなる。

throw式記述場所
//1:ラムダor式形式のメンバーの中
static void test1() => throw new NotImplementeException();
static string test2(string ex)
{
    //2:Null合体演算子(??)の右辺
    var a = ex as string ?? throw new ArgumentException(nameof(obj));
    //3:条件演算子の2,3つ目の引数
    return a.Length == 0 ? "enmpty" :
        a.Length < 5 ? "short" :
        throw new InvalidOperationException("too long");
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0