August 2017
Beginner
566 pages
12h 34m
English
We're going to try to divide by zero, but this might generate errors because division by zero is not allowed. Within the try block, type the following:
var result = (y != 0) ? $"{x / y}" : throw new DivideByZeroException();
What is this saying? Well, we're saying if y does not equal 0, then you can do the division. Then, the division is perfectly legitimate. So we have ? and $ for string interpolation. After this, within curly braces, we do the division. Remember, this is a logical condition: if y does not equal 0, then this chunk of code runs. The result of this is a string that is stored in the result variable. However, if y is equal to 0, then we will use the new DivideByZeroException class and terminate the block. ...
Read now
Unlock full access