0

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);
            }
        }
  • 3
    What's probably happening (if you're testing this in visual studio) is it prints the error message to the console but then the program window closes immediately so you don't have time to see it. If you put a Console.ReadLine() after the end of the catch block then it should pause waiting for you to press Enter before the console window closes. But...don't use exceptions for simple data validation. Exceptions should be for significant errors / failures, primarily. – ADyson Jan 06 '21 at 16:04
  • Does this answer your question? [Preventing console window from closing on Visual Studio C/C++ Console application](https://stackoverflow.com/questions/1775865/preventing-console-window-from-closing-on-visual-studio-c-c-console-applicatio) – Jawad Jan 06 '21 at 16:06

3 Answers3

1

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.

Blindy
  • 65,249
  • 10
  • 91
  • 131
0

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");
Andrei Solero
  • 802
  • 1
  • 4
  • 12
0

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");
    }