✅ Exception handling..
When the method
Run
encounters an instance where number2 == 0
i want it to throw an exception BUT i dont want the program to stop. How does that work?53 Replies
not sure what you mean
the calling code has to handle the exception with a try/catch
You have to catch that exception
Otherwise, the program will crash
So i have to make a
try/catch
on the call to run
?ye
yes
or somewhere in that call stack so the exception gets caught at some point
Right, that does make sense but
Ugh it's so hard to explain
In this code here, down towards the bottom we call the
decimal output = calculationHandler.Run(top);
I could make a try/catch here HOWEVER in the output they expect me to list the numbers i'ts trying to divide with. And they are defined elsewhere
e.g
4 0 / should result in console output
Exception: Divide by zero: 4,00/0,00`so put that information in your custom exception and access it in the catch to print that message
exceptions are just classes, they can hold whatever data you want
I can do that?
So?
Or some such
Or, yeah, put that info in the exception
Can my custom exception print it out?
Exceptions don't "run"
So they cannot print
You can catch that exception and print whatever's stored in it
Ok so in my division class, if the number2 == 0 i throw a new custom exception with those two numbers
For example, yeah
Visual studio made it for me but
Seems right?
Not quite
It's private
How you gonna access that from the outside?
Make those private fields into public properties
Can be get-only
Yep, and maybe call the base constructor to set a message
Hold on this is a lot
What is the method meant to do?
What method?
The constructor?
It's... the constructor
It's what gets called when you
new
that thing uphaha this course has been so goddamn rushed
Angius
REPL Result: Success
Console Output
Compile: 484.101ms | Execution: 33.981ms | React with ❌ to remove this embed.
i feel so overwhelmed
How's that a constructor. I was told that is a method.
Look closely
It has no return type
ah..
And it has the same name as the class
It's a constructor
$structure
So constructors are methods without a return type. They just create an object of what's inside the {}?
They let you do the initial setup of the class, basically
In your example, the code you sent, it prevents you from ever creating an instance that would not have those two numbers
Angius
REPL Result: Failure
Exception: CompilationErrorException
Compile: 272.814ms | Execution: 0.000ms | React with ❌ to remove this embed.
One of two ways to ensure the instance will be given all the necessary data when created
Yes ok but
`
I created my exception for it now
Yep, this will work
yep, starting to look good
The naming does not adhere fully to C# standards, but whatever
It will do
And in my Division class i throw that exception?
Yep
so far so good
Ok so all the way back
How do i access number1 and number2?
Because they're still undefined out here
That
ex
will be the instance of your exception
And it has those two properties you definedLike so?
ex.number1
ooh
OOOH
ex.number2
Okay it's getting way more vividly clear hahah
C# is tricky
:catHeyHello:
It does make sense in the end, though
Yeah just to reiterate what i just did ...
I created an Exception called
AlexDivideByZeroException
which is a subclass of Exception
.
In the exception we defined 2 fields (i think it's called fields or attributes)
We then made a constructor which should hold 2 numbers.
We throw a new exception with became an instance?Two properties, not fields, and certainly not attributes
And you did throw a new instance of this exception
It did not magically become an instance when throwing
right
I think i'm getting the hang of it
luckily i have to create two more
So i can get some more practice 💀
So now i'm trying to solve this
however
4/3
and A + B
result in indexoutofrangeexception
But they should be treated differently
Exception: Invalid Operation 4 / 3
Exception: Invalid token: A A + B
Exception: Invalid token: & 5 & 6
time for a break c:
thanks for the help guys 🫶 time to be suicidal