Multiple Catch Blocks for Different Objects

Medium Exception Handling in OOP
Solve in Playground →

Problem Statement

Read an integer `code`. If it is `1`, throw the integer `404`. If `2`, throw the string literal `”String Error”`. If `3`, throw a `std::runtime_error(“Runtime Error”)`. Write three separate `catch` blocks to handle these exactly. Print ‘Caught Int: [val]’, ‘Caught String: [val]’, or ‘Caught Runtime: [e.what()]’.

Input Format

A single integer code (1, 2, or 3).

Output Format

Print the uniquely caught output.

Constraints

Code will be 1, 2, or 3.

Sample Input

2

Sample Output

Caught String: String Error

Explanation

C++ evaluates catch blocks sequentially. You can route different exception types to entirely different recovery workflows.

Starter Code

#include <iostream>
#include <stdexcept>

int main() {
    int code;
    if (std::cin >> code) {
        try {
            // Throw based on code
        } 
        // Write multiple catch blocks here
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used