---
title: "Exception with argument and specification program"
subject: "Object Oriented Programming"
chapter: 10
key: "semester_3/object_oriented_programming:exception-program-arg-specification"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/exception-program-arg-specification
site: BCT Engineering
---

# Exception with argument and specification program

Object Oriented Programming · Chapter 10: Exception Handling

- Asked in: 2068 Chaitra, 4 marks
- Video: [Exception Specifications in C++ with Theory & Program | By Rohit Kautkar](https://www.youtube.com/watch?v=ecZ0qWs9WP0) — Rohit Kautkar
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/exception-program-arg-specification
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-10

## Question

Write a meaningful program illustrating the use of both Exception with argument and Exception Specification for function.

## Short answer

Define a RangeError class with msg and value. Function checkAge throws RangeError for negative age or int for age>120, with specification throw(RangeError,int). Main catches RangeError to print msg/value, catches int to print age too large.

## Answer

**Exception with argument** – the thrown object carries data (here, a class object with a message and a value), which the handler receives as its argument.
**Exception specification** – a function declares which exception types it may throw: `void f() throw(int, MyError);`. (An empty list `throw()` means it throws nothing. This feature is used in older textbooks and is deprecated in modern C++, but it is still the expected syntax in this syllabus.)

```cpp
#include <iostream>
using namespace std;

class RangeError {                      // exception with argument
public:
    const char *msg;
    int value;
    RangeError(const char *m, int v) : msg(m), value(v) {}
};

// exception specification: this function can throw only RangeError or int
void checkAge(int age) throw(RangeError, int) {
    if (age < 0)
        throw RangeError("Age is negative", age);
    if (age > 120)
        throw age;                      // plain int exception
    cout << "Valid age: " << age << endl;
}

int main() {
    int ages[] = {25, -5, 150};
    for (int i = 0; i < 3; i++) {
        try {
            checkAge(ages[i]);
        }
        catch (RangeError e) {          // receives object with data
            cout << e.msg << " (" << e.value << ")" << endl;
        }
        catch (int n) {
            cout << "Age too large: " << n << endl;
        }
    }
    return 0;
}
```

**Output**

```text
Valid age: 25
Age is negative (-5)
Age too large: 150
```

If `checkAge` threw a type not listed in its specification, `unexpected()` would be called and the program would terminate. Compile with `-std=c++98` (or `c++03`) because dynamic exception specifications were removed in C++17.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is an exception with argument?
   A. The thrown object carries data received by the handler
   B. A catch with no parameters
   C. A throw without a value
   D. A function with arguments
   Answer: A. The thrown object (e.g. RangeError with msg and value) carries information to the handler. (Key point: Exception with argument)

2. What does void checkAge(int age) throw(RangeError, int) mean?
   A. It must throw both
   B. The function may throw only RangeError or int
   C. It never throws
   D. It catches those types
   Answer: B. The specification lists the types the function may throw. (Key point: Specification meaning)

3. What happens if a function throws a type not in its specification?
   A. It is caught by catch(...) automatically
   B. Nothing
   C. unexpected() is called and the program terminates
   D. The compiler converts it
   Answer: C. unexpected() is called and the program ends. (Key point: Unlisted type)

## Related in Object Oriented Programming

- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Simple program illustrating OOP concept](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-simple-program) — Object Oriented Programming, chapter 1
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Square root with exception](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Asqrt-exception-program) — Object Oriented Programming, chapter 10
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1

## Related in other subjects

- [Sum of both diagonals of matrix](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-both-diagonals) — Computer Programming, chapter 7
