Exchange
Exchange
Available on:
• Overview of C++
• Introduction to C++
• OOPS concepts basic
• Basic Syntax and Structure
• Data types and Modifiers
• Variables in C++
• Operators in C++
• sizeof and typedef in C++
• Decision Making
• Loop types
• Storage Classes
• Functions
• Inheritance
• Introduction to Inheritance
• Types of Inheritance
• Order of Constructor Call
• Upcasting
• Polymorphism
• Function Overriding
• Virtual Functions
• Abstract class and Pure Virtual Functions
• Virtual Destructors
• Operator Overloading
• Operator Overloading
• Operator Overloading Examples
• C++ Miscellaneous
• File Streams
• Exception Handling
• Memory Management
• Multithreading
• Initializer List
• Defining Namespace
• STL in C++ →
• C++ Programs →
Compile Time Errors – Errors caught during compiled time is called Compile time
errors. Compile time errors include library reference, syntax error or incorrect class
import.
Run Time Errors - They are also known as exceptions. An exception caught during run
time creates serious issues.
• try
• catch
• throw
Syntax:
try
{
//code
throw parameter;
}
catch(exceptionname ex)
{
//code to handle exception
}
try block
The code which can throw any exception is kept inside(or enclosed in) a try block. Then,
when the code will lead to any error, that error/exception will get caught inside
the catch block.
catch block
catch block is intended to catch the error and handle the exception condition. We can
have multiple catch blocks to handle different types of exception and perform different
actions when the exceptions occur. For example, we can display descriptive messages
to explain why any particular excpetion occured.
throw statement
It is used to throw exceptions to exception handler i.e. it is used to communicate
information about error. A throw expression accepts one parameter and that parameter
is passed to handler.
throw statement is used when we explicitly want an exception to occur, then we can
use throwstatement to throw or generate that exception.
Below program compiles successfully but the program fails at runtime, leading to an
exception.
#include <iostream>#include<conio.h>
using namespace std;
int main()
{
int a=10,b=0,c;
c=a/b;
return 0;
}
The above program will not run, and will show runtime error on screen, because we are
trying to divide a number with 0, which is not possible.
How to handle this situation? We can handle such situations using exception handling
and can inform the user that you cannot divide a number by zero, by displaying a
message.
In the code above, we are checking the divisor, if it is zero, we are throwing an exception
message, then the catch block catches that exception and prints the message.
Doing so, the user will never know that our program failed at runtime, he/she will only
see the message "Division by zero not possible".
This is gracefully handling the exception condition which is why exception handling is
used.
Integer exception
Character exception
The above program is self-explanatory, if the value of integer in the array x is less than
0, we are throwing a numeric value as exception and if the value is greater than 0, then
we are throwing a character value as exception. And we have two different catch blocks
to catch those exceptions.
Generalized catch block in C++
Below program contains a generalized catch block to catch any uncaught
errors/exceptions. catch(...) block takes care of all type of exceptions.
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int x[3] = {-1,2};
for(int i=0; i<2; i++)
{
int ex=x[i];
try
{
if (ex > 0)
throw ex;
else
throw 'ex';
}
// generalised catch block
catch (...)
{
cout << "Special exception\n";
}
}
return 0;
}
Special exception
Special exception
In the case above, both the exceptions are being catched by a single catch block. We
can even have separate catch blocks to handle integer and character exception along
with th generalised catch block.
Standard Exceptions in C++
There are some standard exceptions in C++ under <exception> which we can use in our
programs. They are arranged in a parent-child class hierarchy which is depicted below: