Why won't the second try-catch block propagate an overflowexception?
When anything over 2.147b is entered for the first try-catch block, it successfully automatically throws (and then catches) an overflowexception.
It does not need
But when anything over 2.147b is entered into the console for the second try-catch block, it does not automatically throw the overflowexception. Instead, it throws a formatexception.
The second block needs
to throw overflow. WHY?
Code below:
It does not need
if(int.Parse(input) < 0)if(int.Parse(input) < 0)But when anything over 2.147b is entered into the console for the second try-catch block, it does not automatically throw the overflowexception. Instead, it throws a formatexception.
The second block needs
if(int.Parse(input) < 0)if(int.Parse(input) < 0)Code below:
int? n = null;
string input;
while (n == null)
{
Console.WriteLine("Please enter a positive integer");
try
{
input = Console.ReadLine();
if (int.Parse(input) < 0)
{
Console.Write("Number must be a positive integer. ");
continue;
}
else if (!int.TryParse(input, out _))
{
throw new FormatException();
}
else if (int.TryParse(input, out _))
{
n = int.Parse(input);
}
}
catch (OverflowException o)
{
Console.Write("OverflowException. ");
}
catch (FormatException f)
{
Console.Write("FormatException. ");
}
}
int[] arr = new int[(int)n];
try
{
for (int i = 0; i < (int)n; i++)
{
Console.WriteLine("Enter number {0}", i + 1);
input = Console.ReadLine();
if (!int.TryParse(input, out _))
{
throw new FormatException("FormatException.");
}
else if (int.TryParse(input, out _))
{
arr[i] = int.Parse(input);
}
}
Console.WriteLine("Sum is " + Sum(arr));
}
catch (OverflowException o)
{
Console.WriteLine("OverflowException.");
}
catch (FormatException f)
{
Console.WriteLine("FormatException.");
}
//=======================================
static int Sum(int[] numbers)
{
int sum = 0;
foreach (int number in numbers)
{
sum += number;
}
return sum;
}int? n = null;
string input;
while (n == null)
{
Console.WriteLine("Please enter a positive integer");
try
{
input = Console.ReadLine();
if (int.Parse(input) < 0)
{
Console.Write("Number must be a positive integer. ");
continue;
}
else if (!int.TryParse(input, out _))
{
throw new FormatException();
}
else if (int.TryParse(input, out _))
{
n = int.Parse(input);
}
}
catch (OverflowException o)
{
Console.Write("OverflowException. ");
}
catch (FormatException f)
{
Console.Write("FormatException. ");
}
}
int[] arr = new int[(int)n];
try
{
for (int i = 0; i < (int)n; i++)
{
Console.WriteLine("Enter number {0}", i + 1);
input = Console.ReadLine();
if (!int.TryParse(input, out _))
{
throw new FormatException("FormatException.");
}
else if (int.TryParse(input, out _))
{
arr[i] = int.Parse(input);
}
}
Console.WriteLine("Sum is " + Sum(arr));
}
catch (OverflowException o)
{
Console.WriteLine("OverflowException.");
}
catch (FormatException f)
{
Console.WriteLine("FormatException.");
}
//=======================================
static int Sum(int[] numbers)
{
int sum = 0;
foreach (int number in numbers)
{
sum += number;
}
return sum;
}