Usually we do our best to make sure that our
program does not crash. Today we are going to do the exact opposite,
force it to crash and display an error message. Let’s talk about how we do that, and maybe
more importantly why. I will use the example from the Named arguments
video, it’s a pretty cool video which you should check out if you haven’t already. Here we have a situation where we need the
argument that we send in the be a table, if one were to forget that and simply pass in
values that would lead to the program crashing. When it crashes it will display an error message
which would state that it attempted to index a local value “settings”, a number value. It might not be instantly obvious what the
problem is, and there are other cases where the error message will be even more vague
and difficult to understand. To make it easier for ourselves, we can use
the assert function. It takes two arguments, first an evaluation,
which when it does not evaluate to true will crash the program and display the error message,
which is the second argument. In this case I will use the “type” function,
which will return the type of the variable as a string, and compare the result of that
to the string “table”. In other words, we check and make sure that
the settings argument is indeed a table. If it is not, then we want to display a custom
error message. I will write “Error, table expected, got:”
and concatenate that with the type of the argument. If we run the program you can see that it
crashes and displays our custom error message, letting us know that the problem is the fact
that we did not provide a table. There are other use cases for assert, apart
from providing more clear error messages. We can for example use it to make sure that
specific logic errors cause the program to crash. Logic errors are errors which normally won't
crash your game, only make it function in an unintended way. Let’s say for example that you have an area
where your game takes place. A mistake in your code sets a character to
position negative 100, instead of 100. This may be very difficult to debug, the game
runs perfectly but your character is nowhere to be seen. To prevent this we could add an assert, which
checks that the X value is greater than 0. If it is not, then we want the game to crash
and display an error message which states that the X value can not be negative. Now we can attempt to pass in a negative value
and run the program. As you can see the game now crashes, and displays
our error message. Let’s look at another function which can
be used in a similar way as the assert function, and that function is called “Error”. This is useful for situations where if your
code reaches a specific place, then something has gone terribly wrong and you want the program
to crash. You could for example have a function which
sets the direction of a character. It takes an argument “direction” which
is supposed to be either “left” or “right”. Anything else is not valid and would indicate
that there is a mistake elsewhere in the code. We can add an if statement for left and then
an “elseif” for right. Finally we can add an else, which will catch
everything which is not “left” or “right”. Here we can add an Error, it takes one argument,
the custom error message which we wish to display. I will let us know that the direction needs
to be either “left” or ”right” and then let us know what we actually passed in,
to help with the debugging. If I now try to pass in “up” and run the
program, you can see that it crashes, informing us what went wrong in a very clear way. It is human to make mistakes, some people
watch the entire video without liking, subscribing and joining our discord server. The important thing is to deal with those
errors, using Assert and Error are two ways to help make your code more safe and sound.