why we really need exceptions in c++ ?? is it is necessary to make a mistake?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why we really need exceptions in c++ ?? is it is necessary to make a mistake??

4th Jul 2020, 3:15 PM
Ankit Nainwal
Ankit Nainwal - avatar
2 Answers
0
It's not necessary, it's human and every piece of code has its vulnerabilities... Additionally in many (maybe somebody can confirm ALL) languages, it's easier and cleaner and according to the corresponding code conventions to handle many errors with exceptions...
4th Jul 2020, 4:26 PM
Sandra Meyer
Sandra Meyer - avatar
0
No its not like the way you think. Let me give you an example. There is a code to perform division operation which is simple right? Take 2 inputs and write input1/input2 in cout statement. Bbut what if a weird minded guy comes and gives input like 4 and 0? It will go into infinite loop. The code is not broken, logic is correct then also this thing happens. This is exactly why exception is necessary. I know this thing can be accomplished using if statement but this is just a simple example, In bigger projects you ought to handle exceptions in case a user comes and enters an invalid input. Another example can be entering number where name is expected, you can validate this in try block and can handle exception.
4th Jul 2020, 4:30 PM
Alaska
Alaska - avatar