+ 1
Help me with this output please. I want to output "you can't divide by 0 - error 01; when someone tries to divide by 0 (Solved)
12 ответов
+ 6
You can check value of <b> before moving on with division.
case '/':
if(b == 0) {
cout << "Division by zero\n";
}
else {
// perform division
}
CrazySun03 you posted this question before, and some people already proposed answers, may I ask you why post another one?
https://www.sololearn.com/Discuss/1679738/?ref=app
+ 7
Alright then CrazySun03 , but if you still want to go with Exception way I might be able to help you a bit, which part of it that you find difficult? I'll see if I can shed a little light ...
+ 5
Hey CrazySun03 I just paid a revisit to the Exception chapter, and strangely I feel the same with you, yes I think that chapter could use a little tweak.
I did a test with division by zero, using `try ... catch` but I feel something is odd, errors such as division by zero doesn't seem to captured in `catch` block, I get no output instead XD.
I will get back to you when I have better news to pass 👍
+ 3
CrazySun03 I guess this article is related with, and may add an insight regarding division by zero error handling, I see the general voice there agree to check input before moving on with the operation.
https://stackoverflow.com/questions/15277129/c-divide-by-zero
+ 3
CrazySun03 Excellent! I didn't even think you were really going to dig into it that far 😁
Thanks for sharing what you found, I'm glad you got it. Keep learning and keep sharing 👍
+ 2
Ipang On the another question, i asked how to get (you can't divide by 0).
On this question, I wanted to learn how to use "Exceptions", since I read the article and i didn't understand it very well
I still don't know how Exceptions work, but your answer solved my problem
+ 2
Ipang Well, i'm not very good at english, the C++ tutorial have translation, but the page of Exceptions doesn't.
If you read it, you will se that it have 3 pages, i understand the first two perfectly, but i don't get why i should need 'try' and 'catch'. It seems like i can do the same with only 'throw'
+ 2
Ipang Also i don't know how to use 'try' on a quite longest code
+ 2
Ipang Thank you very match.
If wasn't for you, i wouldn't know that i can use an statement inside of a loop, (like if inside an switch case).
Also thx for the article
+ 1
Ipang I found how it works,
If you use just throw, the program will terminate, and throw a terminate error, followed by a mesagge that you write
If you use try and catch, you can now first, identify the error, and then trow a mesagge that you want, like "you can't do that - Error 99"
+ 1
Eg:
With only throw:
int x = 2;
int y = 1;
if (y < x) {
throw "Error 99";
}
Output = Stopped
Terminate called after throwing an instance of 'char const*'
Error 99
--
If you use try and catch
try {
int x = 2
int y = 1
if ( y < x ) {
throw 99
}
catch (int x)
cout << "You can't do that - Error << x;
}
Output: You can't do that - Error 99
+ 1
no problem dude, i really like programming, so if i have any doubt about something, i will try to understand it