+ 1

How to make the exception send the message after inputting data? C#

I want to make the program send the excpetion message after inputting data. Now when I for example trigger the excpetion after just writing the inputs for x it send the message iimmediately. Can someone help? static void Main(string[] args) { int x; int y; int z; int suma; try { x = Convert.ToInt32(Console.ReadLine()); y = Convert.ToInt32(Console.ReadLine()); z = Convert.ToInt32(Console.ReadLine()); suma = x + y + z; Console.WriteLine(suma); } catch (FormatException) { Console.WriteLine("format exception, exit"); } catch (ArgumentException) { Console.WriteLine("argument exception, exit"); } catch (OverflowException) { Console.WriteLine("overflow exception, exit"); } catch { Console.WriteLine("non supported exception, exit"); } }

6th Dec 2022, 5:53 PM
Filip Gajko
Filip Gajko - avatar
1 Réponse
0
To make the exception send the message after sending the data in the program you provided, you will need to move the Console.WriteLine() statement that outputs the sum of the variables x, y, and z outside of the try block and into the catch block. This will ensure that the message is only printed if an exception is thrown, and that the sum is printed before the exception message. https://code.sololearn.com/cfh9NT7YPtJ9/?ref=app
8th Dec 2022, 2:47 PM
THATguy18
THATguy18 - avatar