</>Learn to Write Code
LearnPseudocodeReal CodeLanguagesVideosCode TalkAbout
Learn → Programming Fundamentals → Errors & Exception Handling

Programming Fundamentals

01Variables02Operators03Input & Output04Sequence05Conditions06Loops07Functions08Objects09Lists10Strings11Errors12Recursion13Inheritance
Lesson 11 of 13·Beginner

Errors & Exception Handling

Wrap risky instructions so your program can handle a problem gracefully instead of crashing.

Related:errorexceptiontrycatchthrow

Programs can fail gracefully instead of crashing

Sometimes an instruction cannot finish the way you hoped: bad input, a broken rule, a missing file. Instead of letting the whole program crash, you can attempt the risky part and handle the problem if it happens.

You can also deliberately raise a problem yourself when your own rule is broken, such as trying to withdraw more money than an account holds.

  • Wrap risky instructions in ATTEMPT.
  • Deliberately THROW PROBLEM when a rule is broken.
  • Handle it with IF PROBLEM OCCURS instead of crashing.
  • Read what went wrong with PROBLEM DETAILS.
Think of a smoke detector: it does not prevent every fire, but it makes sure the failure gets noticed and handled instead of ignored.
Try it: Rewrite the withdrawal example so it throws a problem when withdrawal is a negative number instead of when it exceeds the balance.

Attempt a withdrawal and handle an insufficient balance

The withdrawal is larger than the balance, so the ATTEMPT block throws a problem that IF PROBLEM OCCURS catches and reports.

Pseudocode
CREATE balance AS 50
CREATE withdrawal AS 75

ATTEMPT
    IF withdrawal GREATER THAN balance THEN
        THROW PROBLEM "Insufficient funds"
    END IF
    CHANGE balance TO balance - withdrawal
    SHOW balance
IF PROBLEM OCCURS THEN
    SHOW JOIN "Withdrawal failed: " AND PROBLEM DETAILS
END ATTEMPT

The same idea in Python

Python uses try/except, and raise to deliberately signal a problem.

Python
balance = 50
withdrawal = 75

try:
    if withdrawal > balance:
        raise Exception("Insufficient funds")
    balance = balance - withdrawal
    print(balance)
except Exception as problem:
    print("Withdrawal failed: " + str(problem))

The same idea in JavaScript

JavaScript uses try/catch, and throw with an Error to signal a problem.

JavaScript
let balance = 50;
const withdrawal = 75;

try {
    if (withdrawal > balance) {
        throw new Error("Insufficient funds");
    }
    balance = balance - withdrawal;
    console.log(balance);
} catch (problem) {
    console.log("Withdrawal failed: " + problem.message);
}

The same idea in C#

C# uses try/catch, and throw with an Exception to signal a problem.

C#
int balance = 50;
int withdrawal = 75;

try
{
    if (withdrawal > balance)
    {
        throw new Exception("Insufficient funds");
    }
    balance = balance - withdrawal;
    Console.WriteLine(balance);
}
catch (Exception problem)
{
    Console.WriteLine("Withdrawal failed: " + problem.Message);
}

The same idea in Java

Java uses try/catch, and throw with an Exception to signal a problem.

Java
int balance = 50;
int withdrawal = 75;

try {
    if (withdrawal > balance) {
        throw new Exception("Insufficient funds");
    }
    balance = balance - withdrawal;
    System.out.println(balance);
} catch (Exception problem) {
    System.out.println("Withdrawal failed: " + problem.getMessage());
}

The same idea in C++

C++ uses try/catch, and throw with a std::runtime_error to signal a problem.

C++
int balance = 50;
int withdrawal = 75;

try
{
    if (withdrawal > balance)
    {
        throw std::runtime_error("Insufficient funds");
    }
    balance = balance - withdrawal;
    std::cout << balance << '\n';
}
catch (const std::exception& problem)
{
    std::cout << "Withdrawal failed: " << problem.what() << '\n';
}
Lesson 11 of 13 · Concept
← Previous Lesson: StringsNext Lesson: Recursion →

Learn to Write Code

Learn the idea first, write it in pseudocode, then see how real programming languages express the same logic.

Learn

CurriculumPseudocode StandardLanguagesReal Code

Elsewhere

Code TalkAbout BrianSpread the WordCoders Rock — PracticecodeBetter on YouTubeSupporting files on GitHubAdvertising & Promotions
© 2026 Brian Kittrell, an exteran studios site. Educational content and examples are provided for learning purposes.