CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #135

Which Call Throws

easyExceptions

doubled throws for a negative number. What is printed?

📄 Code

1#include <iostream>
2#include <stdexcept>
3 
4int doubled(int n) {
5 if (n < 0) {
6 throw std::invalid_argument("negative");
7 }
8 return n * 2;
9}
10 
11int main() {
12 try {
13 std::cout << doubled(3) << "\n";
14 std::cout << doubled(-1) << "\n";
15 std::cout << doubled(5) << "\n";
16 } catch (const std::invalid_argument& e) {
17 std::cout << "stopped: " << e.what() << "\n";
18 }
19 return 0;
20}