public class NumberSwapper
public static void SwapNumbers(ref int a, ref int b)
if ((a > 0 && b > int.MaxValue - a) || (a < 0 && b < int.MinValue - a))
throw new OverflowException("Potential integer overflow detected.");
public static void GetInputAndSwap()
Console.Write("Enter A: ");
if (!int.TryParse(Console.ReadLine(), out int a))
Console.WriteLine("Invalid input for A. Please enter an integer.");
Console.Write("Enter B: ");
if (!int.TryParse(Console.ReadLine(), out int b))
Console.WriteLine("Invalid input for B. Please enter an integer.");
SwapNumbers(ref a, ref b);
Console.WriteLine("After Swapping");
Console.WriteLine($"A: {a} and B: {b}");
catch (OverflowException ex)
Console.WriteLine($"Error: {ex.Message}");
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
public static void Main(string[] args)