Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Rectangle Area Calculator | Custom Exception Types
C++ Exception Handling

bookChallenge: Rectangle Area Calculator

Challenge Overview:

You will create a function to calculate the area of a rectangle. The function must throw a custom exception, NegativeDimensionException, if either the width or the height provided is negative. This ensures that only valid, non-negative dimensions are used in area calculations.

Key Points:

  • Define a custom exception class: NegativeDimensionException;
  • Calculate the area using width and height;
  • Throw the exception if width or height is negative;
  • Return the area for valid dimensions.

Example Usage:

try {
    int area = rectangleArea(5, -2);
    // This line will not execute because an exception is thrown
} catch (const NegativeDimensionException& e) {
    std::cout << e.what() << std::endl; // Output: Rectangle dimensions must be non-negative.
}
Завдання

Swipe to start coding

Create a function that calculates the area of a rectangle, ensuring both dimensions are valid.

Your task:

  • Write a function that returns the area of a rectangle using the provided width and height;
  • If either dimension is negative, throw a custom exception to prevent invalid area calculations;
  • Use a custom exception class with a clear error message to indicate the problem.

Follow these requirements:

  • Define a custom exception class for negative dimensions;
  • Implement the area calculation function;
  • Throw the exception if any dimension is negative;
  • Return the calculated area if both dimensions are valid.

Рішення

solution.cpp

solution.cpp

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
single

single

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show me how to define the NegativeDimensionException class?

How should the rectangleArea function be implemented?

What should the exception message say when thrown?

close

Awesome!

Completion rate improved to 6.67

bookChallenge: Rectangle Area Calculator

Свайпніть щоб показати меню

Challenge Overview:

You will create a function to calculate the area of a rectangle. The function must throw a custom exception, NegativeDimensionException, if either the width or the height provided is negative. This ensures that only valid, non-negative dimensions are used in area calculations.

Key Points:

  • Define a custom exception class: NegativeDimensionException;
  • Calculate the area using width and height;
  • Throw the exception if width or height is negative;
  • Return the area for valid dimensions.

Example Usage:

try {
    int area = rectangleArea(5, -2);
    // This line will not execute because an exception is thrown
} catch (const NegativeDimensionException& e) {
    std::cout << e.what() << std::endl; // Output: Rectangle dimensions must be non-negative.
}
Завдання

Swipe to start coding

Create a function that calculates the area of a rectangle, ensuring both dimensions are valid.

Your task:

  • Write a function that returns the area of a rectangle using the provided width and height;
  • If either dimension is negative, throw a custom exception to prevent invalid area calculations;
  • Use a custom exception class with a clear error message to indicate the problem.

Follow these requirements:

  • Define a custom exception class for negative dimensions;
  • Implement the area calculation function;
  • Throw the exception if any dimension is negative;
  • Return the calculated area if both dimensions are valid.

Рішення

solution.cpp

solution.cpp

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
single

single

some-alt