+ 6
Why are exceptions needed ? It basically does the same work as cout and cin..though We can do the same work with these commands?
8 Réponses
+ 12
programs break, if eg a devision by zero occurs.
This is not a user fiendly behaviour.
Exception can handle thinks, one does not think of they can occur.
At the end you are right. There are often many ways to code. Exceptions are regarded as good style.
But also this is not everyones opinion.
+ 9
In very very little cases there is a philosophy
let it crash
It was the topic of a work by Joe Armstrong.(erlang)
What this situation makes special: there are many actors responsible for the result so one crash...
You might think of the Christmas tree in new york.
all lights are independent,
If one light crashes ... let it crash.
normally we dont have such situations.
+ 7
Exceptions are nothing like cout or cin. cin is used to read from the console. cout is used to write to the console. Exceptions allow you to keep running your program after a runtime error, instead of halting.
Now, it is possible to check all data to make sure a runtime error can't occur and, before exceptions existed, we poor programmers had to do that to keep the program running. However, it can be a huge pain to do so requiring error returns through multiple levels of functions/methods. With exceptions, the top level error handling could be reached immediately with zero coding impact on the executing functions.
+ 6
Your little example works. But, what if you have 20 function calls where the bottom one knows there is a problem. It now needs every function above it to abort out of it's normal processing. Maybe as Jamie stated your using a binary library without source.
+ 4
Let's take the case of getting a 0 in denominator.. We applied a throw statement inside an if..... Bt for the same we can apply cout statement inside if else loop like
If (num 2==0)
{ cout<<wrong no. ;}
Else {cout<<num1/num2;}
+ 3
In my opinion its a pure performance thing.
If your error is not fatal, then a library is going to return an error code/enum value etc.(then you can handle the error by easy switch statement), at least i have never seen libraries throwing non fatal exceptions, so that i should handle them.
But if the exception is fatal, performance is not relevant anymore. Library is going to throw and you should try to save the session and rescue as much as possible.