2.35 - Exceptions

Objetive
Create a program to ask the user for two numbers and display their division. Errors must be trapped using "try..catch".

Solution
using System;
public class exercise054
{
    public static void Main()
    {
        int number1;
        int number2;
        int division;
                
        Console.Write("Enter a number to divide: ");
        number1 = Convert.ToInt32(Console.ReadLine());
        
        Console.Write("Enter another number to divide: ");
        number2 = Convert.ToInt32(Console.ReadLine());
        
        try
        {
            division = number1 / number2;
            
            Console.WriteLine("{0} / {1} = {2}", number1, number2, division);
        }
        
        catch (DivideByZeroException)
        {
            Console.WriteLine("Cannot divide by Zero");
            return;
        }
        
    }
}


Popular Posts