Level 1
1 day < 1 min
LeaderboardGuest · Log in
Key point 5 of 8 · Programming Fundamentals
Estimated reading time: 6 min

Data Structures and Memory

Error handling refers to the techniques a program uses to anticipate and respond gracefully to conditions that would otherwise cause it to crash or produce incorrect results, such as invalid input, a missing file, or a failed network connection. The dominant mechanism for this in most modern languages is the exception: when code encounters a condition it cannot handle where it occurs, it 'throws,' or raises, an exception, a signal that immediately interrupts the normal flow of execution and searches outward through the surrounding code for a matching handler. A try block wraps a section of code that might fail, and an accompanying catch block, placed immediately after it, specifies what should happen if an exception is thrown anywhere within that try block, allowing the program to recover, log the problem, show the user a helpful message, or clean up any resources that were in use, rather than terminating abruptly. Some languages also provide a finally block, which runs regardless of whether an exception occurred, commonly used to release resources such as open files or network connections that must be closed no matter what. Used well, this mechanism separates the normal logic of a program from the logic needed to cope with the things that can go wrong, keeping both easier to read.

Food for thought

Why do you think "Data Structures and Memory" was significant in this period?