✅ 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
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
I tried your suggestion but some of the variables are bugged
so let's work with that
what do you mean when you say some variables are bugged
on the variable named option, it says use of unassigned variable 'option'
you'll have to share the code so i can see the variable you're talking about
can't give a full answer if you only show me half the code
$code
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/and what's the exact error it's giving you?
The error is on this line of code while (option != 7);
right, but what exactly does the error say
use of unassigned local variable 'option'
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 itok
so the solution is just to initialize
option
to somethingupdated ushort option; to ushort option = 0;
it's working now, thank you
sure thing
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.
ushort is 2 bytes & uint is 4 bytes plus I don't need that many numbers for the option menu
ushort is fine
this refers more to throwing exceptions. they could (and should) use
TryParse()
, but catching an exception isn't strictly wrongright! 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.seems like a bit of a hassle trying to get TryParse() to work with user input & switch case
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-wiseif 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
switch statements in C# accept way more than numbers
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":
yep
and then you don't need to parse to an integer at all
you can just have a
default:
case that handles invalid inputI 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.
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