why we really need exceptions in c++ ?? is it is necessary to make a mistake?? | SoloLearn: Learn to code for FREE!

+1

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

7/4/2020 3:15:53 PM

decoder

3 Answers

New Answer

+1

Filtering user input to prevent run time errors is one of the hardest things to do. There's an old saying which goes "You can't make anything idiot proof, because idiots are so resourceful." There are other surprises in store for programmer's. Network packets get corrupted, wifis drop out, bluetooth devices become unpaired, power supplies fail, hard drives crash, coffee gets spilled on keyboards, network cables get chewed by animals. Need I go on. Hence we have crc checksums. We test for the right amount of data read from a device, we check for null when requesting memory or opening files. If a function can return an error value then you had better be checking for it. RTFM. Exceptions were introduced because lazy programmers were not checking for error values. See: https://raygun.com/blog/costly-software-errors-history/

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...

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.