Types of Errors in C++ - dummies

By Stephen R. Davis

Two types of errors exist — those that C++ can catch on its own and those that the compiler can’t catch. Errors that C++ can catch are known as compile-time or build-time errors. Build-time errors are generally easier to fix because the compiler points you to the problem, if you can understand what the compiler’s telling you.

Sometimes the description of the problem isn’t quite right (it’s easy to confuse a compiler), but you start to understand better how the compiler thinks as you gain experience.

Errors that C++ can’t catch don’t show up until you try to execute the program during the process known as unit testing. During unit testing, you execute your program with a series of different inputs, trying to find inputs that make it crash. (You don’t want your program to crash, of course, but it’s always better that you — rather than your user — find and correct these cases.)

The errors that you find by executing the program are known as run-time errors. Run-time errors are harder to find than build-time errors because you have no hint of what’s gone wrong except for whatever errant output the program might generate.

The output isn’t always so straightforward. For example, suppose that the program lost its way and began executing instructions that aren’t even part of the program you wrote. (That happens a lot more often than you might think.)

An errant program is like a train that’s jumped the track — the program doesn’t stop executing until it hits something really big. For example, the CPU may just happen to execute a divide-by-zero operation — this generates an alarm that the operating system intercepts and uses as an excuse to terminate your program.

An errant program is like a derailed train in another way — once the program starts heading down the wrong path, it never jumps back onto the track.

Not all run-time errors are quite so dramatic. Some errant programs stay on the tracks but generate the wrong output (almost universally known as “garbage output”). These are even harder to catch since the output may seem reasonable until you examine it closely.