Throwing Objects as Exceptions

Medium Exception Handling in OOP
Solve in Playground →

Problem Statement

Define an `ErrorDetails` struct containing an `int code` and `std::string msg`. Create a `DataProcessor` class with a `process(int val)` method. If `val < 0`, throw an `ErrorDetails` object with code `400` and msg `"NegativeVal"`. Catch it by reference (`const ErrorDetails& e`) and print 'Error [code]: [msg]'. If valid, print 'Valid'.

Input Format

A single integer.

Output Format

Print 'Valid' or the structured error.

Constraints

None

Sample Input

-5

Sample Output

Error 400: NegativeVal

Explanation

You are not limited to primitive types; you can throw and catch entire instantiated objects to pass rich contextual data about the error.

Starter Code

#include <iostream>
#include <string>

struct ErrorDetails {
    // Define properties
};

class DataProcessor {
public:
    void process(int val) {
        // Throw object if negative
    }
};

int main() {
    // Read input and implement try-catch
    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