C++ Exception Handling
Definition
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.
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.
Exception Handling Fundamentals
C++ exception handling is built upon three keywords: try, catch, and
throw. In the most general terms, program statements that you want to monitor
for exceptions are contained in a try block. If an exception (i.e., an error) occurs
within the try block, it is thrown (using throw). The exception is caught, using
catch, and processed.
try {
// try block
}
catch (type1 arg) {
// catch block
Syntax: }
catch (type2 arg) {
// catch block
}
catch (type3 arg) {
// catch block
}
..
.
catch (typeN arg) {
// catch block
}
• #include <iostream> • #include <iostream>
• using namespace std; • using namespace std;
• float division(int x, int y) {
• float division(int x, int y) {
• if( y == 0 ) {
• return (x/y); • throw "Attempted to divide by zero!";
• } • }
• int main () { • return (x/y);
• int i = 50; • }
• int main () {
• int j = 0;
• int i = 25;
• float k = 0; • int j = 0;
• k = division(i, j); • float k = 0;
• cout << k << endl; • try {
• return 0; • k = division(i, j);
• } • cout << k << endl;
• }catch (const char* e) {
• cerr << e << endl;
• }
OUTPUT: • return 0;
Floating point exception (core dumped) • }
OUTPUT:
Attempted to divide by zero!
Catching Class Types
An exception can be of any type, including class
types that you create. Actually, in real-world programs, most exceptions
will be class types rather than built-in types. Perhaps the most common
reason that you will want to define a class type for an exception is to
create an object that describes the error that occurred. This
information can be used by the exception handler to help it process the
error.
// Catching class type exceptions. int main()
#include <iostream> {
#include <cstring> int i;
using namespace std; try {
class MyException { cout << "Enter a positive number: ";
public: cin >> i;
char str_what[80]; if(i<0)
int what; throw MyException("Not Positive", i);
MyException() { *str_what = 0; what = 0; } }
catch (MyException e) { // catch an error
MyException(char *s, int e) {
cout << e.str_what << ": ";
strcpy(str_what, s);
cout << e.what << "\n";
what = e;
}
}
return 0;
};
}