+ 1

I want to make that when someone tries to divide by 0,the output says:you can't, or something like that.How can i do it?(solved)

https://code.sololearn.com/cIS1MI1tme89/?ref=app

6th Feb 2019, 2:55 AM
CrazySun03
CrazySun03 - avatar
8 Antworten
+ 7
https://www.sololearn.com/learn/CPlusPlus/1918/?ref=app
6th Feb 2019, 3:05 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 12
I don't know much c++ but as far as other languages are concerned u can use exception handling and can display required message. You can use try block and catch block.
6th Feb 2019, 3:04 AM
Arushi Singhania
Arushi Singhania - avatar
+ 5
Arushi Singhania that's exactly how it works in C++ too.. CrazySun03 you can use "assert" from <cassert> too. Like this.. assert("you can't do something like that");
6th Feb 2019, 3:30 AM
Mensch
Mensch - avatar
+ 5
If u will not use try and catch, throw will throw an error and ur programme will terminate. To catch that error and to display some msg and to stop ur programme from creating error, you need try and catch block
6th Feb 2019, 10:48 AM
Arushi Singhania
Arushi Singhania - avatar
+ 5
In this case, insert division algorithm in try block that is the part which will throw exception. After it, open catch block, specify the type of exception in parenthesis and then in curly braces, write what u want to display in case of that error.
7th Feb 2019, 3:51 AM
Arushi Singhania
Arushi Singhania - avatar
+ 2
Muhd Khairul Amirin Bin Yaacob Arushi Singhania Thank you so much, but i don't get why 'try' and 'catch' are useful. I think i can do the same with only throw. Where i can use try and catch?
6th Feb 2019, 6:05 AM
CrazySun03
CrazySun03 - avatar
+ 1
Peter.. Can you please be a little more specific? i'm new on c++. When i should use assert?
6th Feb 2019, 6:06 AM
CrazySun03
CrazySun03 - avatar
+ 1
Arushi Singhania Thx, but how should i use try and catch? where? when?
6th Feb 2019, 3:42 PM
CrazySun03
CrazySun03 - avatar