---
title: "Date of birth validation with exceptions"
subject: "Object Oriented Programming"
chapter: 10
key: "semester_3/object_oriented_programming:dob-exception-program"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/dob-exception-program
site: BCT Engineering
---

# Date of birth validation with exceptions

Object Oriented Programming · Chapter 10: Exception Handling

- Asked in: 2073 Shrawan, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/dob-exception-program
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-10

## Question

Write a program to read your Date of Birth and display it. Your program should use multiple exception for day, month and other values not in range using exception class and each exception is handled by separate handler.

## Short answer

Define exception classes DayError, MonthError, YearError. Read day, month, year; throw respective error if out of range (year 1900-2100, month 1-12, day valid for month/leap year). Catch each in separate handler and display invalid value.

## Answer

Assumption: valid year is 1900 to 2100. Separate exception classes are used for day, month and year; each is caught by its own handler.

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

class DayError   { public: int value;  DayError(int v)   : value(v) {} };
class MonthError { public: int value;  MonthError(int v) : value(v) {} };
class YearError  { public: int value;  YearError(int v)  : value(v) {} };

int main() {
    int d, m, y;
    cout << "Enter date of birth (day month year): ";
    cin >> d >> m >> y;
    try {
        if (y < 1900 || y > 2100) throw YearError(y);
        if (m < 1 || m > 12)      throw MonthError(m);

        int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        bool leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
        if (leap) days[1] = 29;
        if (d < 1 || d > days[m - 1]) throw DayError(d);

        cout << "Date of birth: " << d << "/" << m << "/" << y << endl;
    }
    catch (DayError e)   { cout << "Invalid day: "   << e.value << endl; }
    catch (MonthError e) { cout << "Invalid month: " << e.value << endl; }
    catch (YearError e)  { cout << "Invalid year: "  << e.value << endl; }
    return 0;
}
```

**Sample runs**

```text
Enter date of birth (day month year): 15 8 2002
Date of birth: 15/8/2002

Enter date of birth (day month year): 31 4 2002
Invalid day: 31

Enter date of birth (day month year): 10 13 2002
Invalid month: 13
```

## Check yourself

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

1. Why does the program define DayError, MonthError and YearError?
   A. To avoid using try
   B. To store the date
   C. So each type is caught by its own handler
   D. To format output
   Answer: C. Different classes let separate catch blocks handle each type of invalid value. (Key point: Separate exception classes)

2. How does it validate the day?
   A. Always checks 31
   B. Checks only d > 0
   C. Checks the year
   D. Compares with days[m-1], with February set to 29 in a leap year
   Answer: D. The days array gives the month's length and is adjusted for leap years. (Key point: Day check)

3. Which condition identifies a leap year in the program?
   A. (y%4==0 && y%100!=0) || y%400==0
   B. y%4==0 only
   C. y%100==0
   D. y%2==0
   Answer: A. Divisible by 4 but not 100, or divisible by 400. (Key point: Leap year rule)

4. What does the program print for 31 4 2002?
   A. Invalid month: 4
   B. Invalid day: 31
   C. Date of birth: 31/4/2002
   D. Invalid year: 2002
   Answer: B. April has 30 days, so the day is out of range and DayError is thrown. (Key point: Output for 31 4 2002)

## Related in Object Oriented Programming

- [Handling multiple exceptions](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amultiple-exceptions) — Object Oriented Programming, chapter 10, asked 2×
- [Exception with argument and specification program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-program-arg-specification) — Object Oriented Programming, chapter 10
- [Class template with multiple template types](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-multiple-types) — Object Oriented Programming, chapter 9
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [Square root with exception](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Asqrt-exception-program) — Object Oriented Programming, chapter 10
- [Display N characters using default arguments](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3An-chars-default-args-program) — Object Oriented Programming, chapter 3

## Related in other subjects

- [Exception class hierarchy from Throwable](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aexception-class-hierarchy-throwable) — Object Oriented Analysis and Design, chapter 4
- [CT secondary should not be kept open](https://www.bctengineer.com/question.md?key=semester_4%2Felectrical_machines%3Act-secondary-open-true-false) — Electrical Machines, chapter 2
- [Partial and multiple correlation: 8 values](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Apartial-multiple-corr-sums-8-values) — Probability and Statistics, chapter 5, asked 2×
- [College records with nested date structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-college-estdate) — Computer Programming, chapter 8
