← C++ from Scratch Lesson 47 of 62 · 5 XP

Kinds of errors

Theory

Machine translation, not yet reviewed by a native speaker.

Errors come in three kinds — and they're caught in different ways.

1. Compile errors — the program doesn't even build: a missing ;, a typo in a name, a wrong type. The compiler writes the file, the line and the reason:

main.cpp:5:20: error: expected ';' after expression

Read the first error: the rest are often its consequences. Warnings (warning) aren't a trifle: in C++ they're almost always a future bug. The -Wall flag turns on the main ones (it's on here).

2. Runtime errors — the program built, but it crashed or behaved wildly:

  • going out of an array's bounds or dereferencing nullptr is undefined behaviour (UB): the program may crash, or it may silently corrupt data. In the browser, going far outside the program's memory gives "Memory access out of bounds…", but reading through nullptr in the browser doesn't crash: in WebAssembly address 0 is an ordinary memory cell, and *p silently returns garbage. On real Linux the same program crashes with Segmentation fault. That's why "it didn't crash for me" proves nothing;
  • dividing an integer by 0 — a crash;
  • an endless loop — the program is stopped by a time-out;
  • overflowing an int — also UB.

3. Logic errors — everything runs, but the answer is wrong: < instead of <=, a forgotten break, a wrong formula. The computer doesn't know what you meant — these are caught with tests and debugging.

Protection: warnings turned on, const wherever possible, STL containers instead of bare arrays, input validation, and small functions that are easy to check one at a time.

Sign up for free to mark finished lessons and track your progress.