CPP05
Main topics
Repetition and Exceptions (try, throw & catch#include <iostream>
int main() {
int dividend, divisor, result;
std::cout << "Enter the dividend: ";
std::cin >> dividend;
std::cout << "Enter the divisor: ";
std::cin >> divisor;
try
{
if (divisor == 0)
{
throw "Division by zero is not allowed!"; // Throw an exception
}
result = dividend / divisor;
std::cout << "Result: " << result << std::endl;
}
catch (const char* errorMessage)
{
std::cerr << "Error: " << errorMessage << std::endl;
}
return 0;
}Last updated