TYPES OF EXCEPTION IN C++
When executing any code in C++, there may be possibility of having different error. So those errors in the code are made by the programmer, input error or any other error.
When any kind of error occurs in the code, C++ stops the execution and generate an error message, basically if we say in technical term, C++ will throw an exception.
WHAT IS EXCEPTION IN C++?
A C++ exception is a kind of response to a situation that occurs while a program is executing, like for example if we divide any number by zero. Exception provides an easy way to take control on any part of the program and also to transfer control to other part of the program. Exception in C++ is entirely built on three keywords i.e. try, catch and throw.
· throw — When a problem shows up, the program will throws an exception. It is all done by using throw keyword.
· catch — When a user want to handle the problem, a program catches an exception with exception handler. For catching an exception catch keyword is used.
· try — A block of code is identified by try block for which particular exception used to get activated. It’s followed by one or more catch blocks.
Throwing Exceptions
Using throw statement, Exceptions can be thrown anywhere within a code. The throw exception determines:
- A type for the exception
- Type of the result of the expressions
- The type of exception thrown
Following is an example of throwing an exception when dividing by zero condition occurs −
double division(int a, int b) {
if( b == 0 ) {
throw “Division by zero condition!”;
}
return (a/b);
}
Catching And Try Exceptions
The catch and try exceptions work together. The try block followed by catch block catches any exception. We can specify the type of exception we want to catch and this is all determined by exception declaration following by the keyword catch.
For Example:
try {//protected code} catch(ExceptionName e) {//code to handle ExceptionName exception}
Above code will catch an exception of ExceptionName type. We must put an ellipsis, if we want to catch block should handle any type of exceptionthat is thrown in try block, between the parentheses enclosing the exception declaration as follows –
try {//protected code} catch(...) {//code to handle any exception}
The try
and catch
keywords come in pairs.
C++ Standard Exceptions
C++provides a list of standard exceptions defined in <exception> which we can use in any programs. These are arranged in a parent-child class hierarchy shown below
Std::exception- to handle errors through throw exception it provides consistent interface.
Std::bad_alloc- exception thrown by allocation function
Std::bad_cast- when dynamic_cast to reference tupe fails to run time check this exception is thrown.
Std::bad_typeid- when a typeid operator is applied to a dereferenced null pointer value of a polymorphic type, this exception is thrown.
Std::bad_exception- exception thrown by C++ runtime.
Std::logic_error- It reports error.
Std::runtime_error- it reports error whn an event is beyond the scope of the program.
Std::domain_error- reports domain error.
Std::invalid_arguement- reports an invalid argument.
Std::length_error- exception thrown to report length errors.
Std::out_of_range- exception thrown to out of range errors.
Std::overflow_error- exception thrown to arthmatic overflow errors.
Std::range _error- exception thrown to range errors.
Std::underflow_error- exception thrown to arthmatic underflow errors.
Handle Any Type of Exceptions (…)
To handle any type of exception, we can use three dots(…) syntax inside the catch block.:
try {
int age = 15;
if (age >= 18) {
cout << “Access granted — you are old enough.”;
} else {
throw 505;
}
}
catch (…) {
cout << “Access denied — You must be at least 18 years old.\n”;
}
we can define our own exceptions by inheriting and overriding exception class functionality.