EXCEPTION HANDLING
-
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();
}
}
}
} -
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();
}
}
}
} -
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();
}
}
}
} -
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();
}
}
}
}Try this: public void test() { try { Console.WriteLine("Please enter the variables : "); Int32 valueOne = ConvertLine(Console.ReadLine()); Int32 valueTwo = ConvertLine(Console.ReadLine()); Console.WriteLine("Answer : {0}", valueOne + valueTwo); Console.ReadLine(); } catch (Exception e) { Console.WriteLine("Message : {0}", e.Message); Console.ReadLine(); } } private int ConvertLine(string value) { try { if (String.IsNullOrEmpty(value) == false) { return Convert.ToInt32(value); } Console.Error.WriteLine("Empty field is not supported : default value used 0"); } catch (Exception exception) { Console.Error.WriteLine(String.Format("{0} : default value used 0", exception.Message)); } return 0; }