Programming Search⌘K

Error concepts

What an error is, the strategies to report it, and the practices that decide where errors go.

Concepts

  • Compile-time and run-time errors

    A compile-time error is found before the program runs, such as a syntax or type error. A run-time error is found only when the bad operation runs, such as an index past the end of an array.

    intro
  • Error

    An error is a state in which a program cannot do what it was asked to do. Some errors are expected, such as bad input, and the program reports them. Others come from bugs in the code.

    intro
  • Error codes

    An error code is a number that a function returns, or stores in a known place, to tell the caller which error happened. The caller must test it after each call.

    core
  • Error handling strategies

    A program can report an error with an exception, an error value, an error code, a special value, a callback or a stop of the program. Each language prefers one strategy, and each has a cost.

    core
  • Error propagation

    Error propagation passes an error from the function that finds it up the call stack to a function that can handle it, often with more context added on the way.

    core
  • Recoverable and unrecoverable errors

    A recoverable error is one that a correct program expects and handles, such as a missing file. An unrecoverable error is usually a bug, and the safe action is to stop.

    core

Tasks

  • Define a custom error type

    Report a bad port number with an error type of your own that keeps the text and the reason as fields, and let the caller pick out that type from the other errors.

    10 languagescore
  • Log an error

    Handle an error where it happens by writing one log record at the error level, with the context and the cause as escaped fields, and then go on with the next item.

    10 languagescore
  • Wrap an error with context

    Catch the error of a parser, add the place of the bad setting, and throw or return a new error that keeps the first one as its cause, so a caller can still find it.

    10 languagescore

Practices

  • Writing error messages

    Write each error message so that its reader learns what went wrong, where it went wrong and how to fix it, in words that this reader knows.

    core