Bailing out with fatalError and precondition
It’s comforting to think that in the code you write, everything will always happen as expected, and your program can handle any eventuality. However, sometimes things can go wrong – really wrong. A situation could arise that you know is possible but don’t expect to ever happen, and the program should terminate if it does. In this recipe, we will look at two issues like this – fatalError
and precondition
.
Getting ready
Let’s reuse our example from the previous recipe – we have an object that can be used to classify video game reviews, based on how many stars out of 10 the review gave the video game. However, let’s simplify its use, and say that we only intend for a classifier object to classify one, and only one, video game review.
How to do it...
Let’s set up our video game classifier to only be used once, and only accept ratings out of 10:
- Define the classification...