Advertisement
AliElSallab

C# Exception Exericse

Jul 2nd, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication26
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int x = 0;
  14.             int y = 0;
  15.             try
  16.             {
  17.  
  18.                 Console.WriteLine("Enter 1st no ");
  19.                 x = Convert.ToInt32(Console.ReadLine());
  20.                 Console.WriteLine("Enter 2nd no ");
  21.                 y = Convert.ToInt32(Console.ReadLine());
  22.                 Console.WriteLine("Sum: " + (x + y));
  23.                 Console.WriteLine("Difference:  " + (x - y));
  24.                 Console.WriteLine("Product:  " + (x * y));
  25.                 Console.WriteLine("Quotient:  " + (x / y));
  26.                 Console.WriteLine("Remainder: " + (x % y));
  27.             }
  28.             catch (FormatException e)
  29.             {
  30.  
  31.                 Console.WriteLine("Not an integer");
  32.  
  33.             }
  34.  
  35.             catch (OverflowException e)
  36.             {
  37.                 Console.WriteLine("Number too big or too small");
  38.             }
  39.             catch (DivideByZeroException e)
  40.             {
  41.                 if (x == 0)
  42.                     Console.WriteLine("Quotient: undefined quantity");
  43.                 else if (x > 0)
  44.                     Console.WriteLine("Quotient: +infinity");
  45.                 else if (x < 0)
  46.                     Console.WriteLine("Quotient: -infinity");
  47.  
  48.             }
  49.  
  50.             catch (Exception e) //best practice
  51.             {
  52.                 Console.WriteLine(e.Message);
  53.             }
  54.          
  55.  
  56.  
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement