Kindly check the below program.
/* Brief Decsription : This program checks whether the entered chatacter is a numeral or no.
I have used the IsDigit Method. The issue here is that, the input from keyboard is a string,
whereas the IsDigit method needs a character. Hence I have converted the string input to a character.
Now, when I enter any number from 0 to 9, it perfoms the functionality of addition and also if I
enter any character it hroes an Exception. But if I enter any 2 digit number, then also it throws an
Exception, which is system generated.
This is because of that converison to character.
Kindly provide a solution wherein I can enter numerals greater than 9 without error */
using System;
namespsace TestException3
{
Class program
{
static void Main(string args[])
{
try
{
string s1,s2,ans;
char type,type1;
Console.WriteLine("please enter the variables : ");
s1=Console.ReadLine();
s2=Console.ReadLine();
type=Convert.ToChar(s1);
type1=Convert.ToChar(s2);
if(!(char.IsDigit(type)) || !(char.IsDigit(type1)))
{
throw new Exception("Please Enter numbers only");
}
else
{
ans=Convert.ToString(Convert.ToInt32(s1)+Convert.ToInt32(s2));
Console.WriteLine(Answer : {0}",ans);
Console.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("Message : {0}", e.Message);
Console.ReadLine();
}
}
}
}