We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
C++ Exception Handling
Exception Handling in C++ is a process to handle runtime errors. We perform exception
handling so the normal flow of the application can be maintained even after runtime errors. In C++, exception is an event or object which is thrown at runtime. All exceptions are derived from std::exception class. It is a runtime error which can be handled. If we don't handle the exception, it prints exception message and terminates the program. Advantage It maintains the normal flow of the application. In such case, rest of the code is executed even after exception. C++ Exception Classes In C++ standard exceptions are defined in <exception> class that we can use inside our programs. The arrangement of parent-child class hierarchy is shown below: Prime Ministers of India | List of Prime Minister of India (1947-2020)
All the exception classes in C++ are derived from std::exception class. Let's see the list of C++ common exception classes.
Exception Description
std::exception It is an exception and parent class of all standard C++ exceptions.
std::logic_failure It is an exception that can be detected by reading a code.
std::runtime_error It is an exception that cannot be detected by reading a code.
std::bad_exception It is used to handle the unexpected exceptions in a c++ program.
std::bad_cast This exception is generally be thrown by dynamic_cast.
std::bad_typeid This exception is generally be thrown by typeid.
std::bad_alloc This exception is generally be thrown by new.
C++ Exception Handling Keywords
In C++, we use 3 keywords to perform exception handling: o try o catch, and o throw try: represents a block of code that can throw an exception.(i.e is used to place the code that may occur exception.) catch: represents a block of code that is executed when a particular exception is thrown. (i.e is used to handle the exception.) throw: Used to throw an exception. Also used to list the exceptions that a function throws, but doesn’t handle itself.
C++ try/catch example
#include <iostream> using namespace std; float division(int x, int y) { if( y == 0 ) { throw "Attempted to divide by zero!"; } return (x/y); } int main () { int i = 25; int j = 0; float k = 0; try { k = division(i, j); cout << k << endl; }catch (const char* e) { cerr << e << endl; } return 0; } Output: Attempted to divide by zero! QN//Following are main advantages of exception handling over traditional error handling. 1) Separation of Error Handling code from Normal Code: In traditional error handling codes, there are always if else conditions to handle errors. These conditions and the code to handle errors get mixed up with the normal flow. This makes the code less readable and maintainable. With try catch blocks, the code for error handling becomes separate from the normal flow.
2) Functions/Methods can handle any exceptions they choose: A function can
throw many exceptions, but may choose to handle some of them. The other exceptions which are thrown, but not caught can be handled by caller. If the caller chooses not to catch them, then the exceptions are handled by caller of the caller. In C++, a function can specify the exceptions that it throws using the throw keyword. The caller of this function must handle the exception in some way (either by specifying it again or catching it) 3) Grouping of Error Types: In C++, both basic types and objects can be thrown as exception. We can create a hierarchy of exception objects, group exceptions in namespaces or classes, categorize them according to types.