✅ Try catch exception.

What is the best way to keep doing a do while loop if an exception has been caught? Atm if I create the exception code within the do while loop the variables are not then declared even though they are The following example works but exits out of the do while loop. variables try{ do{code} while{code} } catch(Exception){code}
31 Replies
mg
mg7d ago
switch them around try/catch inside the loop, instead of the loop inside the try/catch not sure what you mean about the variables not being declared "are not then declared even though they are" doesn't quite make sense
77_105_114_111
77_105_114_111OP7d ago
I tried your suggestion but some of the variables are bugged
mg
mg7d ago
so let's work with that what do you mean when you say some variables are bugged
77_105_114_111
77_105_114_111OP7d ago
on the variable named option, it says use of unassigned variable 'option'
mg
mg7d ago
you'll have to share the code so i can see the variable you're talking about
77_105_114_111
77_105_114_111OP7d ago
No description
mg
mg7d ago
can't give a full answer if you only show me half the code $code
MODiX
MODiX7d ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
77_105_114_111
77_105_114_111OP7d ago
//internal class Program
{
private static void Main(string[] args)
{
ushort option;
long additionNum1;
long additionNum2;
long result;

do
{
try
{

Console.WriteLine("Welcome to the custom calculator.");
Console.WriteLine("----------------------------------------------");
Console.WriteLine("1. Press 1 for + Addition: ");
Console.WriteLine("2. Press 2 for - Subraction: ");
Console.WriteLine("3. Press 3 for * Multiplication: ");
Console.WriteLine("4. Press 4 for / Division: ");
Console.WriteLine("5. Press 5 for % Modulo: ");
Console.WriteLine("6. Press 6 for ² Square root: ");
Console.WriteLine("7. Press 7 to exit calculator: ");
Console.WriteLine();


option = ushort.Parse(Console.ReadLine());

if (option == 1)
{

Console.WriteLine();
Console.WriteLine("Enter the addition numbers: ");
additionNum1 = long.Parse(Console.ReadLine());
additionNum2 = long.Parse(Console.ReadLine());
result = additionNum1 + additionNum2;
Console.WriteLine(additionNum1 + " + " + additionNum2 + " = " + result);
Console.WriteLine();

}
Console.WriteLine();
}
catch (Exception)
{
Console.WriteLine("Invalid input.");
}
}

while (option != 7);



Console.WriteLine("Thank you for using the custom calculator.");
Console.ReadKey();
}
}
//internal class Program
{
private static void Main(string[] args)
{
ushort option;
long additionNum1;
long additionNum2;
long result;

do
{
try
{

Console.WriteLine("Welcome to the custom calculator.");
Console.WriteLine("----------------------------------------------");
Console.WriteLine("1. Press 1 for + Addition: ");
Console.WriteLine("2. Press 2 for - Subraction: ");
Console.WriteLine("3. Press 3 for * Multiplication: ");
Console.WriteLine("4. Press 4 for / Division: ");
Console.WriteLine("5. Press 5 for % Modulo: ");
Console.WriteLine("6. Press 6 for ² Square root: ");
Console.WriteLine("7. Press 7 to exit calculator: ");
Console.WriteLine();


option = ushort.Parse(Console.ReadLine());

if (option == 1)
{

Console.WriteLine();
Console.WriteLine("Enter the addition numbers: ");
additionNum1 = long.Parse(Console.ReadLine());
additionNum2 = long.Parse(Console.ReadLine());
result = additionNum1 + additionNum2;
Console.WriteLine(additionNum1 + " + " + additionNum2 + " = " + result);
Console.WriteLine();

}
Console.WriteLine();
}
catch (Exception)
{
Console.WriteLine("Invalid input.");
}
}

while (option != 7);



Console.WriteLine("Thank you for using the custom calculator.");
Console.ReadKey();
}
}
mg
mg7d ago
and what's the exact error it's giving you?
77_105_114_111
77_105_114_111OP7d ago
The error is on this line of code while (option != 7);
mg
mg7d ago
right, but what exactly does the error say
77_105_114_111
77_105_114_111OP7d ago
use of unassigned local variable 'option'
mg
mg7d ago
ok, so, the problem is that, although you know Console.WriteLine() won't throw an exception, the compiler doesn't let's pretend that every time the loop runs, an exception is thrown before option = ushort.Parse(Console.ReadLine()) is reached then option will never get a value assigned to it
77_105_114_111
77_105_114_111OP7d ago
ok
mg
mg7d ago
so the solution is just to initialize option to something
77_105_114_111
77_105_114_111OP7d ago
updated ushort option; to ushort option = 0; it's working now, thank you
mg
mg7d ago
sure thing
Adam Vincent
Adam Vincent7d ago
no need to bother with ushort in .NET just use int ushort gets expanded to 32bit anyways, and integer math is faster. also, generally speaking, exceptions are for exceptional circumstances. with your console app example, a user entering a bad option wouldn't generally be considered exceptional.
77_105_114_111
77_105_114_111OP7d ago
ushort is 2 bytes & uint is 4 bytes plus I don't need that many numbers for the option menu
Cattywampus
Cattywampus7d ago
ushort is fine
mg
mg6d ago
this refers more to throwing exceptions. they could (and should) use TryParse(), but catching an exception isn't strictly wrong
Adam Vincent
Adam Vincent6d ago
right! we read the same thing from different perspectives. It is fine. But in general, int is the framework default convention. I prefer to stick to convention.
77_105_114_111
77_105_114_111OP6d ago
seems like a bit of a hassle trying to get TryParse() to work with user input & switch case
mg
mg6d ago
TryParse() really only needs to be in the input portion combining it with continue should get you something similar to what you have now, functionality-wise
77_105_114_111
77_105_114_111OP6d ago
if the user input is a string data type then it does not work with switch case because switch case only excepts numeric data. I guess the only good time to use TryParse() is when working with if else statements
mg
mg6d ago
switch statements in C# accept way more than numbers
77_105_114_111
77_105_114_111OP6d ago
so instead of case 1: case: 2 we can have case stringName: case stringName2? Ok I understand instead of case: 1 we can change it to case "1":
mg
mg6d ago
yep and then you don't need to parse to an integer at all you can just have a default: case that handles invalid input
77_105_114_111
77_105_114_111OP6d ago
I created 2 custom calculator apps one with if statements & Try Catch. The other with switch case & TryParse(), 3 kilo bytes difference, not much but then again if I were to use the TryParse() with a much bigger app then it adds up Thanks again for the support, cheers.
mg
mg6d ago
sure thing! i'd advise that source code size, binary size, and lines of code aren't very good indicators of complexity. with modern storage, unless you're working in a constrained environment like a microcontroller, don't worry too much about size

Did you find this page helpful?