Handling Exceptions - CSharp Custom Type

CSharp examples for Custom Type:Exception

Description

Handling Exceptions

Demo Code

using System;/*from  w ww. j ava  2  s  .  c  om*/
using static System.Console;
class Program
{
   static void Main(string[] args)
   {
      Write("What is your age? ");
      string input = Console.ReadLine();
      try
      {
         int age = int.Parse(input);
         WriteLine($"You are {age} years old.");
      }
      catch (OverflowException)
      {
         WriteLine("Your age is a valid number format but it is either too big or small.");
      }
      catch (FormatException)
      {
         WriteLine("The age you entered is not a valid number format.");
      }
      catch (Exception ex)
      {
         WriteLine($"{ex.GetType()} says {ex.Message}");
      }
   }
}

Result


Related Tutorials