Error Handling
In a procedural language the error handling is either done by checking the return value of a function or checking some other variable that contains the error. The pseudo code can look like: result = function1() ; if ( result < 0 ) //Handle error function2() ; if ( ErrorCode == 100 ) //Handle error The code can easily become very messy. Another way to handle errors and this is usually a feature in object oriented programming is to use Exceptions. The above will instead look like: try { result = function1() ; function2() ; } catch ( Exception except ) { //Any error in the try block causes control to come //to the catch block. }
File: except1.cpp
Output: $ ./a.exe Error divisor is zero. Divide by zero.There is a slight disadvantage to the exception handling and that is the control movies to the catch block and it may be difficult to retry the function calls as we are out of the normal coding block.
We can also have multiple catch statements corresponding to our try block.
File: except2.cpp
Output: $ $ g++ except2.cpp ; ./a.exe Error divisor is zero. Integer Exception./a.exe Error divisor is zero. Divide by zero.If an exception is thrown and it is not caught in that function then it will be thrown again to the calling function and the process can repeat.
File: except3.cpp
$ g++ except3.cpp ; ./a.exe Integer ExceptionIf the exception is not thrown then the main program aborts.
File: except4.cpp
$ g++ except4.cpp ; ./a.exe terminate called after throwing an instance of 'int' Aborted (core dumped)We can have nested try catch blocks also.
File: except5.cpp
Output: $ g++ except5.cpp ; ./a.exe Integer Exception Outer Integer ExceptionExercises
1)
File: ex1.cpp
2)
File: ex2.cpp
We can use the "..." to catch an exception of any type .
File: except6.cpp
Output: $ g++ except6.cpp ; ./a.exe Default Handler.We can also throw an object of our own class.
File: except7.cpp
If we have a hierarchy then we can throw a derived exception and catch it with the base class.
File: except8.cpp
If we list the classes in a hierarchy we need to make sure we list the derived classes first. The following produces a warning.
File: except9.cpp
$ g++ except9.cpp ; ./a.exe except9.cpp: In function ‘int main()’: except9.cpp:36:5: warning: exception of type ‘MyException’ will be caught by earlier handler [-Wexceptions] 36 | catch ( MyException& e1 ) | ^~~~~ except9.cpp:31:5: note: for type ‘MyExceptionBase’ 31 | catch ( MyExceptionBase& e1 ) | ^~~~~ ErrorIt should be written as:
File: except10.cpp
Standard Exceptions
C++ provides standard exceptions that can be used to catch exceptions thrown by the language.
File: mem1.cpp
$ g++ mem1.cpp ; ./a.exe Standard exception: std::bad_allocWe can also derive our own class from the exception class.
File: mem2.cpp
Exception specifications allow us to specify if a method throws an exception or not. In the below code the "method3" is called but the exception is not caught even though there is a try catch block. This is because the "method3" threw an exception even though we declared that it does not do so.
File: except11.cpp