Lesson 23 of 24

Exceptions: try, catch, throwExceptions: try, catch and throw

9 min 35 XP

throw மூலம் பிழையை எழுப்பி, try/catch மூலம் பிடிக்கலாம். Standard exceptions: invalid_argument, out_of_range, runtime_error. stoi() தவறான text-க்கு invalid_argument எறியும்.

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

n வரிகள், ஒவ்வொன்றிலும் இரண்டு text மதிப்புகள். stoi மூலம் எண்ணாக மாற்றி வகுத்தல் (int) விடையை print செய்யுங்கள். எண் இல்லையெனில் "Invalid number", 0-ஆல் வகுத்தால் "Divide by zero" (நீங்கள் throw செய்ய வேண்டும்). Input: 3 20 4 7 0 x 2 Output: 5 Divide by zero Invalid number

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.