In this video, we'll see how we can throw and catch multiple exceptions.
Suppose we have a function such as the one in this slide.
The function is called calculate miles per gallon,
and it expects the number of miles and the number of gallons as parameters.
And it just simply divides miles by gallons and returns the result.
This function will fail if the number of gallons is 0 because we'd have a divide by 0 error,
that's what we did in the previous video.
But it would also produce incorrect results if either miles
or gallons is negative.
In this case, we can check for both of these situations
and throw a different exception for each case.
Let's see how we can write this function so it does just that.
The code is pretty much what you would expect. We check if gallons is 0.
And if it is, we throw an integer 0 exception.
We also check if either miles or gallons are negative. And if they are,
we throw a std string object initialized to the string negative value error.
Otherwise, we simply perform the division and return the result.
Let's see how we would call this function.
Notice that in the try block, we're calling calculate miles per gallon
and that function may or may not throw an exception.
If it does not, then we simply assign the result of the function to miles per gallon,
display it, and then we display buy at the end of the program, and we're done.
However, what if calculate miles per gallon does throw an exception.
Remember, it could throw an int or a std string.
So we create two catch handlers, one for each exception type.
And we write our exception handling code in the catch blocks.
If an exception is thrown, then one of these catch handlers will handle it.
If we don't handle a particular exception type here, then c++
will terminate this function and proceed up the call stack looking for a handler
until it finds one or the program terminates.
It's also possible to have a catch-all handler.
This catch handler will fire no matter what type of exception is thrown.
If this is used, it's common to place it as the last of your catch blocks
since it really serves to catch anything that wasn't handled before.
Notice the three dots or ellipsis in the catch all parameter list.
You don't have access to the exception object thrown in this case,
but you can deal with the exception in the body of the catch-all block.
Now let's head over to the IDE,
and we'll rewrite that calculate miles per gallon to throw multiple exceptions.
Okay. So I'm in the CodeLite IDE. I'm in the section 18 workspace.
And I'm in the MPG function multiple exceptions project.
What we've got is the same calculate MPG function
that is simply throwing an exception when gallons was 0.
And what we want to do is we want to extend this a little bit
so that will also throw a different exception
when miles or gallons is less than 0. Okay.
So right now we've got it throwing 0 if gallons is 0,
and we can simply add another check that says if miles is less than 0
or gallons is less than 0,
then we want to throw an exception here. Now it can't be another integer.
We're already throwing an integer type.
So in this case, I simply want to throw a std string,
and I'll just initialize it to something like
negative value error or something like that,
and let me include string up here.
That's it. So in this case, if we're going to get a division by 0 error right here
with gallons equals 0, I'm throwing the integer.
In this case, if I've got an illegal negative value, I'm just going to throw a std string,
that's been initialized to that string negative value error.
That's it. Now down here, if I run this now and throw a string,
it's going to terminate, right, because I'm not catching a string.
So what I need to do down here is add another catch handler for that std string.
So I can add that right down here, and I can say I'm catching a string by reference.
We can call it anything we like. I just -- I always call them ex for exception,
a lot of people call them e., whatever you like,
it's just a variable name.
And in this case, I'm going to display to standard error,
the actual exception, which is a string, so I don't really have to type anything here for the string.
It's just going to display that string value. That's in that exception object.
That's it.
So let's try this. Let's run it,
and let's do something like 120 divided by 0, which gives us the
try to divide by 0 error.
And now we've got a buy, so that's correct.
And now let's try to throw on negative.
So let's say negative 1
and 123, negative value error, just like we expected.
And we'll test it with
negative 1 for gallons or negative anything actually,
there's your negative value error.
That's it, simple as that. So you can see here we're throwing multiple exception types.
And down here, we're catching multiple exception types now.
If I had a catch-all handler here, if I didn't have this handler right here,
and I simply had a catch
something like this with the ellipsis
and not have the string here,
then that would certainly catch the string. I just wouldn't have access to the object. That's all.
Okay. So that's it for this video. I'll see you in the next video.