C Academy · Lesson

Exception Handling in C

Implement robust error-handling strategies without built-in exception handling.

Lesson 3 of 310 steps

Exception Handling in C is a free C Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

Introduction to Exception Handling

Exception Handling in C

Unlike languages like C++ and Java, C does not have built-in exception handling.

In this lesson, you will learn:

  • How to handle errors using return values.
  • How to use setjmp and longjmp for exception handling.
  • How to use errno for system error handling.
Exception Handling in C — illustration 1

Handling Errors with Return Values

The simplest way to handle errors in C is to use return values.

Example:

int divide(int a, int b) { if (b == 0) return -1; // Error code return a / b; }

The caller checks the return value for errors.

Example: Error Handling with Return Values

This program checks for division by zero using return values.

#include <stdio.h>

int divide(int a, int b) {
    if (b == 0) {
        printf("Error: Division by zero!\n");
        return -1;
    }
    return a / b;
}

int main() {
    int result = divide(10, 0);
    if (result != -1) {
        printf("Result: %d\n", result);
    }
    return 0;
}

Using setjmp and longjmp for Error Handling

The setjmp and longjmp functions allow non-local jumps, simulating exception handling.

Steps:

  • Use setjmp() to mark a recovery point.
  • Use longjmp() to jump back when an error occurs.

Example: Using setjmp and longjmp

This program uses setjmp and longjmp for error handling.

#include <stdio.h>
#include <setjmp.h>

jmp_buf buffer;

void errorHandler() {
    printf("Error occurred! Jumping back to safe state.\n");
    longjmp(buffer, 1);
}

int main() {
    if (setjmp(buffer) == 0) {
        printf("Executing main program...\n");
        errorHandler();
    } else {
        printf("Recovered from error.\n");
    }
    return 0;
}

Using errno for System Errors

The errno variable stores error codes from system calls.

Example:

#include

Functions like fopen() set errno when errors occur.

Example: Using errno for File Handling

This program checks errno when opening a non-existent file.

#include <stdio.h>
#include <errno.h>

int main() {
    FILE *file = fopen("nonexistent.txt", "r");
    if (!file) {
        perror("Error opening file");
    }
    return 0;
}

Best Practices for Error Handling

To manage errors effectively:

  • Check return values of functions.
  • Use errno for system errors.
  • Use setjmp and longjmp only when necessary.

Summary

In this lesson, you learned:

  • How to handle errors using return values.
  • How setjmp and longjmp simulate exception handling.
  • How to use errno for system error detection.

This concludes the Debugging and Error Handling section in C!

Exception Handling in C — illustration 10
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Exception Handling in C” lesson free?

Yes — the full text of “Exception Handling in C” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.

What will I learn in “Exception Handling in C”?

Implement robust error-handling strategies without built-in exception handling. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Exception Handling in C” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Debugging with GDB
  2. Common Runtime Errors
  3. Exception Handling in C
← Back to C Academy