Question
Edit the C# Code to solve the problem: The program parses the input into integer and catch possible problems with the value provided. Update this
Edit the C# Code to solve the problem: The program parses the input into integer and catch possible problems with the value provided. Update this code to use TryParse() method. Run it. Remove try-catch block completely. Run it
using System;
using static System.Console;
namespace HandlingExceptions
{
class Program
{
static void Main(string[] args)
{
WriteLine("Before parsing");
Write("What is your age? ");
string input = 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}");
}
WriteLine("After parsing");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started