Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Age Validator | Custom Exception Types
C++ Exception Handling

bookChallenge: Age Validator

Challenge Overview:

You will use custom exception types in C++ to validate user input for age. Implement a function named validateAge(int age) that checks whether an age value is valid. An age is valid if it is between 0 and 150, inclusive. If the age is not within this range, your function must throw a custom exception called InvalidAgeException.

Requirements:

  • Define a custom exception class named InvalidAgeException following C++ best practices;
  • Implement the validateAge(int age) function to check the validity of the input;
  • Throw InvalidAgeException if the age is less than 0 or greater than 150.

Example:

try {
    validateAge(200); // Invalid age
} catch (const InvalidAgeException& e) {
    std::cout << "Caught exception: " << e.what() << std::endl;
}

You will gain hands-on experience with custom exception handling and input validation in C++.

Aufgabe

Swipe to start coding

Your task is to write a function that checks if an age value is valid. The valid range for age is from 0 to 150, inclusive. If the age is outside this range, your function must signal an error using a custom exception type. You need to define this custom exception class yourself, following standard C++ conventions for exception types.

Lösung

solution.cpp

solution.cpp

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show me how to define the `InvalidAgeException` class?

What should the `what()` message say for the exception?

Can you provide an example of how to use `validateAge` in a program?

close

Awesome!

Completion rate improved to 6.67

bookChallenge: Age Validator

Swipe um das Menü anzuzeigen

Challenge Overview:

You will use custom exception types in C++ to validate user input for age. Implement a function named validateAge(int age) that checks whether an age value is valid. An age is valid if it is between 0 and 150, inclusive. If the age is not within this range, your function must throw a custom exception called InvalidAgeException.

Requirements:

  • Define a custom exception class named InvalidAgeException following C++ best practices;
  • Implement the validateAge(int age) function to check the validity of the input;
  • Throw InvalidAgeException if the age is less than 0 or greater than 150.

Example:

try {
    validateAge(200); // Invalid age
} catch (const InvalidAgeException& e) {
    std::cout << "Caught exception: " << e.what() << std::endl;
}

You will gain hands-on experience with custom exception handling and input validation in C++.

Aufgabe

Swipe to start coding

Your task is to write a function that checks if an age value is valid. The valid range for age is from 0 to 150, inclusive. If the age is outside this range, your function must signal an error using a custom exception type. You need to define this custom exception class yourself, following standard C++ conventions for exception types.

Lösung

solution.cpp

solution.cpp

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 2
single

single

some-alt