0% found this document useful (0 votes)
12 views3 pages

EXP-13 Exception Handling

The document explains exception handling in C++, which is the process of managing unexpected events during program execution using try, catch, and throw keywords. It provides examples demonstrating how to handle division by zero and array out-of-bounds errors. The examples illustrate the use of multiple catch statements to handle different types of exceptions.

Uploaded by

shiv.prasad2049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

EXP-13 Exception Handling

The document explains exception handling in C++, which is the process of managing unexpected events during program execution using try, catch, and throw keywords. It provides examples demonstrating how to handle division by zero and array out-of-bounds errors. The examples illustrate the use of multiple catch statements to handle different types of exceptions.

Uploaded by

shiv.prasad2049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

13.

Exception Handling in C++


 An exception is an unexpected event that occurs during program execution. For example,
divide_by_zero = 7 / 0;
 The above code causes an exception as it is not possible to divide a number by 0.
 The process of handling these types of errors in C++ is known as exception handling.
 In C++, we handle exceptions with the help of the try and catch blocks, along with
the throw keyword.
 try - code that may raise an exception
 throw - throws an exception when an error is detected
 catch - code that handles the exception thrown by the throw keyword
 Note: The throw statement is not compulsory, especially if we use standard C++ exceptions.

Example 1: C++ Exception Handling


// program to divide two numbers
// throws an exception when the divisor is 0

#include <iostream>
using namespace std;

int main() {

double numerator, denominator, divide;

cout << "Enter numerator: ";


cin >> numerator;

cout << "Enter denominator: ";


cin >> denominator;

try {

// throw an exception if denominator is 0


if (denominator == 0)
throw 0;

// not executed if denominator is 0


divide = numerator / denominator;
cout << numerator << " / " << denominator << " = " << divide << endl;
}

catch (int num_exception) {


cout << "Error: Cannot divide by " << num_exception << endl;
}
return 0;
}

Output 1
Enter numerator: 72
Enter denominator: 0
Error: Cannot divide by 0
Output 2
Enter numerator: 72
Enter denominator: 3
72 / 3 = 24

Example 2: C++ Multiple catch Statements

#include <iostream>
using namespace std;

int main() {

double numerator, denominator, arr[4] = {0.0, 0.0, 0.0, 0.0};


int index;

cout << "Enter array index: ";


cin >> index;

try {

// throw exception if array out of bounds


if (index >= 4)
throw "Error: Array out of bounds!";

// not executed if array is out of bounds


cout << "Enter numerator: ";
cin >> numerator;

cout << "Enter denominator: ";


cin >> denominator;

// throw exception if denominator is 0


if (denominator == 0)
throw 0;

// not executed if denominator is 0


arr[index] = numerator / denominator;
cout << arr[index] << endl;
}

// catch "Array out of bounds" exception


catch (const char* msg) {
cout << msg << endl;
}

// catch "Divide by 0" exception


catch (int num) {
cout << "Error: Cannot divide by " << num << endl;
}

// catch any other exception


catch (...) {
cout << "Unexpected exception!" << endl;
}

return 0;
}
Output 1
Enter array index: 5
Error: Array out of bounds!
Output 2
Enter array index: 2
Enter numerator: 5
Enter denominator: 0
Error: Cannot divide by 0
Output 3
Enter array index: 2
Enter numerator: 5
Enter denominator: 2
2.5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy