Menu
Coddy logo textTech

What are Exceptions?

Part of the Logic & Flow section of Coddy's Dart journey — lesson 38 of 65.

When you run a program, things don't always go as planned. Sometimes your code encounters unexpected situations that it can't handle normally - these are called exceptions.

An exception is a runtime error that occurs while your program is executing. Unlike syntax errors that prevent your code from running at all, exceptions happen when your program is already running but encounters a problem it doesn't know how to solve.

Common situations that cause exceptions in Dart include trying to convert invalid text to a number (which creates a FormatException), accessing a list element that doesn't exist, or dividing by zero. When an exception occurs and isn't handled properly, your entire program will crash and stop running.

The key insight is that exceptions are predictable - you can anticipate where they might occur and prepare your code to handle them gracefully. This prevents crashes and allows your program to continue running even when something goes wrong.

Cheat sheet

An exception is a runtime error that occurs while your program is executing, unlike syntax errors that prevent code from running at all.

Common exceptions in Dart include:

  • FormatException - trying to convert invalid text to a number
  • Accessing a list element that doesn't exist
  • Dividing by zero

When an exception occurs and isn't handled properly, your entire program will crash and stop running. Exceptions are predictable and can be handled gracefully to prevent crashes.

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow