Exceptions in C++
Exceptions in C++
• Types of Errors
– Syntax
– Logical
– Run-time
• Exceptions are errors that occur at run time
– Running out of memory
– Not being able to open a file
– Out of bounds index to a vector
– Unable to read file content
– Unable to connect to data base/network
– ……………………….
Exceptions
• Exceptions: Are errors that occur at run-time
• Exception handling: A systematic, object oriented approach
to handling errors generated by C++ classes.
• History (method used in past) of managing/handling run-
time errors
– If(somefun() == ERROR_RET_VAL)
// handle the error of call error-handler function
else
//proceed normally
If(anotherfun() == NULL)
// handle the error of call error-handler function
else
//proceed normally
Drawback of this approach
Every Call to function must be examined by the program
Surrounding each function call with if-else statements and adding statements
To handle error or call an error handle routine
Require a lot of code for error management. Error handling code and
application logic is mixed
The problem becomes more complex when classes are used, since errors may
takes place without a function being explicitly called
Ex: SomeClass obj1, obj2, obj3……….
The constructors are called implicitly, there is no return value to be checked