#include <cassert>
#include <iostream>
#include <stdexcept>
// Function using assertion
int divideWithAssert(int a, int b) {
assert(b != 0 && "Divisor cannot be zero");
return a / b;
}
// Function using exception
int divideWithException(int a, int b) {
if (b == 0) {
throw std::invalid_argument("Divisor cannot be zero");
}
return a / b;
}
int main() {
try {
std::cout << "Assert: 10 / 2 = " << divideWithAssert(10, 2) << std::endl;
std::cout << "Exception: 10 / 2 = " << divideWithException(10, 2) << std::endl;
// Uncomment to see assertion failure
// std::cout << "Assert: 10 / 0 = " << divideWithAssert(10, 0) << std::endl;
std::cout << "Exception: 10 / 0 = " << divideWithException(10, 0) << std::endl;
}
catch (const std::exception& e) {
std::cerr << "Exception caught: " << e.what() << std::endl;
}
return 0;
}