C# DivideByZeroException
Program.cs
int n = 515, d = 0, result = 0;
Console.WriteLine(result = n / d);
Output
kodingwindow@kw:~/csharp$ dotnet run
Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.
    at Program.<Main>$(String[] args) in /home/kodingwindow/csharp/Program.cs:line 2
kodingwindow@kw:~/csharp$ 
C# program to handle the DivideByZeroException
Program.cs
int n = 515, d = 0, result = 0;
try
{
    Console.WriteLine(result = n / d);
}
catch (DivideByZeroException e)
{
    Console.WriteLine(e.Message);
}
Output
kodingwindow@kw:~/csharp$ dotnet run
Attempted to divide by zero.
kodingwindow@kw:~/csharp$ 
Advertisement