skipter

try-catch C# Fundamentals

Jun 2nd, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. using System;
  2. namespace ConsoleApp8
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             int number = 0;
  9.             while (true)
  10.             {
  11.                 try
  12.                 {
  13.                     number = int.Parse(Console.ReadLine());
  14.                     Console.WriteLine("It is a number.");
  15.                     break;
  16.                 }
  17.                 catch (Exception ex)
  18.                 {
  19.                     Console.WriteLine("Invalid input!");
  20.                     break; // without break it will wait for another number  
  21.                     // throw; - showing the exception/error
  22.                 }
  23.             }
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment