Lesson 23 of 24

Exceptions: try, catch and throw

9 min 35 XP

Raise an error with throw and handle it with try/catch. Standard exceptions include invalid_argument, out_of_range and runtime_error; stoi() throws invalid_argument for bad text.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Challenge

+35 XP

Read n lines of two text values. Convert them with stoi and print the integer division result. Print "Invalid number" for non-numbers and "Divide by zero" for a zero divisor (throw it yourself). Input: 3 20 4 7 0 x 2 Output: 5 Divide by zero Invalid number

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.