EXCEPTION HANDLING
USING C++
BSCS 2ND SEMESTER
COURSE INSTRUCTOR RIMSHA RAO
EXCEPTION HANDLING
• When executing C++ code, different errors can occur: coding errors made by
the programmer, errors due to wrong input, or other unforeseeable things.
• When an error occurs, C++ will normally stop and generate an error
message. The technical term for this is: C++ will throw an exception (throw
an error).
• Exception is an event which occurs during execution of program that
disrupts normal flow of program
HOW TO HANDLE THE EXCEPTION:
EXCEPTION HANDLING
• Exception Handling 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.
• exception is an event or object which is thrown at runtime..
• All exceptions are derived from 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 HANDLING: CONTINUED
• Example of Exception like Divide by zero, Accessing array element
beyond its limit, running out of memory etc.
• Exception handling mechanism consists of followin parts:
• 1) Find the problem (Hit the Exception)
• 2) Inform about its occurrence (Throw the exception)
• 3) Receive error information(Catch the exception)
• 4) Take proper action(Handle the exception)
WHY EXCEPTION HANDLING
• Seperation of Error handling code from normal code
• functions can handle any exceptions they choose
• Grouping of error types
EXCEPTION HANDLING: CONTINUED
• Exception Handling can be done in 3 ways:
• 1) try block: try block is used to place the code that may occur
exception. Exception are thrown inside the try block..
• 2) catch block: catches an exception which is thrown from try block.
The catch keyword indicates the catching of an exception. It is used to
handle the exception. It defines action to be taken when exception
occur
• 3) throw keyword: throws an exception when a problem is detected.
EXAMPLE WITHOUT EXCEPTION
HANDLING
• #include<iostream>
• void main()
• (
• int a,b;
• a=10;
• b=a/0; //exception occurs
• cout<<"result: "<<b;
• 1 Here program will be terminated you will not get output because exception occurs
at line 6 and flow of program comes out of main() function without executing line 7
Exception Handling
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b;
try {
if (true) { // always throws in this case
throw "Division by zero error!";
}
b = a / 0; // This line is never reached due to throw above
}
catch (const char* msg) {
cout << "Exception caught: " << msg << endl;
}
return 0;
}
• #include <iostream>
• using namespace std;
• int main() {
• int a, b;
• cout << "Enter numerator: ";
• cin >> a;
• cout << "Enter denominator: ";
• cin >> b;
• try {
• if (b == 0) {
• throw "Division by zero is not allowed.";
• }
• cout << "Result = " << a / b << endl;
• }
• catch (const char* msg) {
• cout << "Error: " << msg << endl;
• }
• return 0;
MULTIPLE CATCH
CATCH ALL STATEMENTS
• In some cases it is not feasible to write multiple catch blocks for each kind of
exception in such cases we can write single catch block wi catches all the
exceptions
⚫ syntax:
• catch(....)
•{
• statements;
• ..................
•}
DEFINE NEW EXCEPTION
• In this user can create and throw its own exception with the help
of throw statement
BUILT IN EXCEPTION CLASS
• C++ provides built-in exception classes in the Standard Library to help
handle different types of errors in a structured way.
• These classes are defined in the <exception> and <stdexcept>
headers.