Crashes


Some of you may have noticed a problem with the previous chapter’s program. Try entering this into the prompt and see what happens.

  1. Lispy Version 0.0.0.0.3
  2. Press Ctrl+c to Exit
  3. lispy> / 10 0

Ouch. The program crashed upon trying to divide by zero. It’s okay if a program crashes during development, but our final program would hopefully never crash, and should always explain to the user what went wrong.

walterwhite

Walter White • Heisenberg

At the moment our program can produce syntax errors but it still has no functionality for reporting errors in the evaluation of expressions. We need to build in some kind of error handling functionality to do this. It can be awkward in C, but if we start off on the right track, it will pay off later on when our system gets more complicated.

C programs crashing is a fact of life. If anything goes wrong the operating system kicks them out. Programs can crash for many different reasons, and in many different ways. You will see at least one Heisenbug.

But there is no magic in how C programs work. If you face a really troublesome bug don’t give up or sit and stare at the screen till your eyes bleed. Take this chance to properly learn how to use gdb and valgrind. These will be more weapons in your tool-kit, and after the initial investment, save you a lot of time and pain.