初級 › Attempted to divide by zero の原因と直し方 › パターン2
Attempted to divide by zero の原因と直し方
整数の除算・剰余演算で除数が0のときに発生します。
1 class Program { 2 static void Main(string[] args) { 3 int a = 10; 4 int b = __; ^ 5 System.Console.WriteLine(a % b); 6 } 7 }
Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.
at Program.Main(String[] args) in Program.cs:line 5
空欄を埋めてビルドを通す
剰余演算子%も内部的には除算を行うため、0で割ろうとすると同じ例外になります。
次の問題