© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
10 replies
zzz

catching exception but skipping an iteration

using System.Security.Cryptography.X509Certificates;

namespace Caclculator
{
    class Program
    {
        static void Main(string[] args)
        {

            int numTotal;
            int count = 0;

            Console.WriteLine("Welcome to my calculator.");
            Console.WriteLine("How many numbers are in your calculation?");
            numTotal = int.Parse(Console.ReadLine());
            int[] numbers = new int[numTotal];
            while (count < numTotal - 1)
            {
                for (int i = 0; i < numTotal; i++)
                {
                    Console.WriteLine($"Enter number {i + 1}");
                    try
                    {
                        numbers[i] = int.Parse(Console.ReadLine());
                        count = count + 1;
                    }
                    catch (Exception) 
                    {
                        Console.WriteLine("I said a number lil bro :/");
                    }
                }
            }
        }
    }
}
using System.Security.Cryptography.X509Certificates;

namespace Caclculator
{
    class Program
    {
        static void Main(string[] args)
        {

            int numTotal;
            int count = 0;

            Console.WriteLine("Welcome to my calculator.");
            Console.WriteLine("How many numbers are in your calculation?");
            numTotal = int.Parse(Console.ReadLine());
            int[] numbers = new int[numTotal];
            while (count < numTotal - 1)
            {
                for (int i = 0; i < numTotal; i++)
                {
                    Console.WriteLine($"Enter number {i + 1}");
                    try
                    {
                        numbers[i] = int.Parse(Console.ReadLine());
                        count = count + 1;
                    }
                    catch (Exception) 
                    {
                        Console.WriteLine("I said a number lil bro :/");
                    }
                }
            }
        }
    }
}

my code works fine other than when the user doesnt input a number and my exception error prints it skips to the next number instead of asking them to reinput it
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

catching an exception in an async Task [Answered]
C#CC# / help
4y ago
✅ Is throwing and catching an exception in an UnmanagedCallersOnly method valid?
C#CC# / help
9mo ago
✅ Catching exception at each function or globally?
C#CC# / help
3y ago