When a line of code executes, it might not work sometimes.Errors and failures occurring can be counted throughout the application. In fact, a large part of programming is reacting to that. But some of them are more predictable than others. If users are asked for values, an assumption has to be made at some point that every single user will do some or the other mistake. They might leave something blank that they're not suppose to leave blank. They might enter a letter where they are supposed to enter a number, those sorts of things.
There are other kinds of errors that are much less predictable, and it's arguable that testing for them constantly would put a performance hit on the application. So every time, when another piece of memory is allocated from the free store, memory has to be checked whether it is available or not.
Most of the time, checking these sorts of errors every single time is an actively bad thing to do which takes time and slows the application performance, and these things are really unusual, really kind of unexpected. And so that leads to a best practice that says different kinds of errors should be handled in different ways.
The best advantage in C++ when compared to C language is Exception handling. Basically, there are few errors which cannot be detected by the compiler. They are:
- Run-time errors.
- Compile time errors.
As these errors are not identified by the compiler, they are termed as "
Exceptions". Exceptions occurs during the time of execution of the program.
- Errors occur at "run time" and "compile time" but, Exceptions occurs mostly at "run-time".
- Developer should detect the exception. Errors can be handled by the compiler.
Every time an error occurs, it is not possible to rectify them which will slow down the application performance. To overcome the problem, exceptions are used. Exceptions are nothing but the expected errors which are tested and found solutions before the program gets executed.