Reputation: 33
I am trying to include an exception handling in to this small sample of code. When I am prompted to input the conversionType, I tried to input strings which are supposed to trigger the catch code and print out the error message, but instead the code just shuts down like any other errors, suggesting that the error was not caught by the try catch blocks. I am still learning how exception handling works in C#. So is there anyway to correctly catch the exception and prevent the code from crashing?
static void Main(string[] args)
{
int conversionType;
double number;
Console.WriteLine("Choose the type of conversion:\n" +
"1.Celsius to Fahrenheit\n" +
"2.Fahrenheit to Celsius");
try
{
conversionType = Convert.ToInt32(Console.ReadLine());
if (conversionType == 1)
{
Console.WriteLine("Enter the Temperature in Celsius: ");
number = Convert.ToDouble(Console.ReadLine());
number = number * 9 / 5 + 32;
Console.WriteLine("Temperature in Fahrenheit: {0:00.0}°F", number);
}
else if (conversionType == 2)
{
Console.WriteLine("Enter the Temperature in Fahrenheit: ");
number = Convert.ToDouble(Console.ReadLine());
number = (number - 32) * 5 / 9;
Console.WriteLine("Temperature in Celsius: {0:00.0}°C", number);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
Upvotes: 0
Views: 1070
Reputation: 61
I think what you wanna do is multiple usage of the function within a loop. You can catch different exception types like this and add a finally block.
static void Main(string[] args)
{
int conversionType;
double number;
do
{
try
{
Console.WriteLine("Choose the type of conversion:\n" +
"1.Celsius to Fahrenheit\n" +
"2.Fahrenheit to Celsius");
conversionType = Convert.ToInt32(Console.ReadLine());
if (conversionType == 1)
{
Console.WriteLine("Enter the Temperature in Celsius: ");
number = Convert.ToDouble(Console.ReadLine());
number = number * 9 / 5 + 32;
Console.WriteLine("Temperature in Fahrenheit: {0:00.0}°F", number);
}
else if (conversionType == 2)
{
Console.WriteLine("Enter the Temperature in Fahrenheit: ");
number = Convert.ToDouble(Console.ReadLine());
number = (number - 32) * 5 / 9;
Console.WriteLine("Temperature in Celsius: {0:00.0}°C", number);
}
}
catch (FormatException fe)
{
Console.WriteLine("Format exception:" + fe.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// do something additional for all cases
Console.WriteLine("Try again? (Y/N): ");
}
}
while (Console.ReadLine().ToUpper() == "Y");
}
Upvotes: 0
Reputation: 802
You should put a Console.ReadLine();
on the end of the code for you to see the exception.
And Also, you can add the code below inside your try statement just to make sure the program will catch every exception.
int conversionType;
double number;
Console.WriteLine("Choose the type of conversion:\n" +
"1.Celsius to Fahrenheit\n" +
"2.Fahrenheit to Celsius");
Upvotes: 0
Reputation: 67457
You shouldn't use exceptions for this, you have functions like int.TryParse
and double.TryParse
that return a boolean signifying whether or not they succeeded.
Upvotes: 1