[Autogenerated] Here's an example of a very simple application.
That allows us to calculate the result of simple expressions, just something.
That has a left side, the right side.
And Some operator let me run that first to demonstrate what it does.
So if I go ahead and build, that and run that I can then add some numbers.
For example, like four plus nine,
and get 13 I can even do Other things with spaces like
calculate the product of 12 and three.
So we have these Very simple way of calculating these very simple expressions.
However, what happens if I go ahead.
And decide, to divide by zero.
What we'll get.
Is an exception.
I'll get an exception here saying, hey, this is division by zero,
we can't really do that I'm a processor.
I have no idea how to do that kind of thing.
So we want to handle, that in some way.
So first let's take a quick, look at the existing code So in our main function,
we have an input here which is Just something we get
here using the get S function.
To get some kind of expression from the user.
And then We call this helper function called parsers
expression which takes the input as a string,
and then provides the result as an expression object
which contains the left right?
And The operator in question if the operator is invalid,
then we can just say so otherwise,
we can call the dual function which is supposed to provide the calculation.
So what kali does it gets that expression object,
and the expression object has three fields.
One of them,
is the operator which is used here in a very simple switch statement,
and then we have the expression left and right,
which we are using here,
to make the actual calculation the parsing of the
expression is not super interesting, but here's the gist of it.
We have a string as input, we are looking for one of these characters here.
To represent the operators.
If we don't find any of those, the return values.
End pause.
Which is not a valid position, in the string,
then we return the expression as is and it,
is already initialized with a zero, in the expression operator.
Otherwise we take the left part of the operate operator here by
converting the string on the left side of the operator.
To an integer.
We take the operator itself.
And place, that in the operator, field, and the same thing,
is happening with the right value, just taking whatever is after the operator.
And translating that to an integer.
So other things could actually go wrong here.
But I'm going to uh to to disregard that for now.
So here's the expression structure.
Very simple one.
We have a left value, a right value both of integers.
And the operators, are presented as a simple character where the default is back,
slash zero which equals zero just meaning an invalid operator.
That's all there.
Is here, in the this code Now, how can we handle that division by zero.
Obviously one way, is to check the right value explicitly for zero.
And then two return some kind of error to the call That's definitely possible
but assume that in practice things are much more complex.
It could be part of a very complex expression which perhaps is not really
here might be a different code that is out of my control.
Perhaps let's add a Try, accept here as a way,
to try to Figure out whether we have an exception on our hands.
So we have a try accept block here.
Let me write that properly.
And accept can accept one of three possible values.
One of them, is exception,
execute handler which says I would like to handle whatever exception.
Is here, and I'm willing.
To kind of _______.
That exception, and move on my merry way.
So I can say error and in the This case,
we know that its supposed to be division by zero.
We don't expect any other errors, but let's just write error here.
And be done with it.
So now we expect the application not to _____.
Let's try that If we do something simple.
That doesn't cause an exception, everything.
Is Fine.
Let's try.
To divide by zero again.
And we can see We get an error here.
We're not crushing, more Instead we're getting an error We're returning a zero.
Because after, after what we've seen here,
in the code the return value here, is zero.
So that's one way of handling this exception.
However, maybe that is a bit too much.
Maybe it's possible if the code is more complex,
that the drive block might also contain calling other functions.
And so on.
So maybe we get a different exception here.
It's better.
To be safe here, and say, well, if the exception,
code is something like divide by zero.
Then Definitely we want to handle,
that But otherwise we dot don't really want to handle,
that because we don't know exactly what to do and It's not a Good idea.
To handle something.
We know nothing about.
So if the exception, is in fact, integer, divide by zero.
Then fine.
Let's just handle it.
Otherwise.
We can use a different constant called exception.
Continue, search which says I don't, want to handle,
the exception,
the code here will not run and Instead the call stack
would be searched for a different handler,
that might be able to handle, that different exception,
that occurred.
And that is a better way of working.
Obviously,
the expression here starts to become a bit complex and
its possible to take all that thing, and put that in a separate file function.
Typically, that has the name filter function.
It represents our ability.
To examine the exception.
Then decide whether we would like to handle it or not by
returning one of these two values.
Either we would like to handle the exception or we
don't want to handle the exception.
We like the handler.
To continue searching for other potential handlers.
So that's a better way of handling exceptions.
We don't want to _______ everything because at least in
user mode you can _______ everything.
And If you do that essentially,
you might have errors happening in your application.
And you simply disregarding that So that's probably not a Good idea.
You might lose data You might have some other kind of corruption later on.
So it's better.
To only handle what you know how to handle.
In fact, crashing as the other option.
Is also good because at least it means you won't be doing
any more damage in your application.
So that's a Better way of working there.
Is a third constant you can use which is called exception,
continue execution, which says I managed,
to fix the exception in some way.
Let's continue running like trying to run the same instruction again,
this is something you should almost never use because it
really depends on how the compiler allows you or generates
the code but there is only one.
In fact, scenario where this makes sense,
which is related to virtual addresses, which might be reserved,
but not yet.
Committed.
And When we get the exception of a page fault,
we are able to Uh well, its actually an access violation,
that The way it's being propagated.
To user mode then we can perform the committee of memory,
and I've shown such an example.
In the memory management course, in the these Windows 11,
internal series So I refer you to that to see the entire example.
But this is a more common case where handing exceptions
might be something we we can actually, do