0% found this document useful (0 votes)
18 views

Module 5 C++

C++
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Module 5 C++

C++
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 5

Exception Handling: Introduction to Exception - Benefits of Exception handling- Try


and catch blockThrow statement- Pre-defined exceptions in C++:

Exception Handling:
1.Exception:
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).

2.Basics of Exception Handling:

There are two types of exceptions in C++


1. Synchronous: Exceptions that happen when something goes
wrong because of a mistake in the input data or when the
program is not equipped to handle the current type of data it’s
working with, such as dividing a number by zero.
2. Asynchronous: Exceptions that are beyond the program’s
control, such as disc failure, keyboard interrupts, etc.

3. Exception Handling Mechanism:

#include <iostream>
#include <stdexcept>

using namespace std;

int main() {
int a, b;
cout << "Enter 2 numbers: ";
cin >> a >> b;

cout << a / b;

Cout<<”End of Program”;
}
Scenario: a = 3 and b = 0

Input:
 The user inputs 3 for a and 0 for b.
Execution:
 The program tries to execute the division 3 / 0.
Exception:
 Division by zero is undefined and will result in a runtime error. In
most systems, this will cause the program to terminate.

Exception handling in C++ allows a program to detect and handle exceptional


conditions (like runtime errors) in a controlled manner. The mechanism uses three
keywords: try, throw, and catch.

Key Components

 try block: This block contains code that might throw an exception.
 throw statement: This statement is used to signal the occurrence of an
exceptional condition. It can be used to throw an exception.
 catch block: This block contains code to handle the exception. It catches and
processes the exception thrown by the throw statement.

Syntax:

try {
// Block of code to try
throw exception; // Throw an exception when a problem arise
}
catch () {
// Block of code to handle errors
}

Example:
#include <iostream>
#include <stdexcept>

using namespace std;

int main()
{
int a, b;
cout << "Enter 2 numbers: ";
cin >> a >> b;

try
{
if (b == 0)
{
throw "Divide by zero error";
}
cout << a / b;
}
catch (const char* ch)
{
cout << "Error occurred: " << ch;
}

cout << "End of program";


return 0;
}

Scenario: a = 3 and b = 0

Input:
 The user inputs 3 for a and 0 for b.
Execution:
 The program enters the try block.
 It checks if b is zero. Since b is zero, it throws a const char* exception
with the message "Divide by zero error".
 The catch block catches this exception and prints the message "Error
occurred: Divide by zero error".
 After the catch block, the program prints "End of program".

Output

Enter 2 numbers: 3 0
Error occurred: Divide by zero errorEnd of program

4.Multiple catch Statements:


#include <iostream>
using namespace std;

int main()
{
try
{
throw 5;
}
catch (int x) {
cout << "Caught " << x;
}
catch (const char* ch) {
cout << "Default Exception\n";
}
return 0;
}

Output:
Caught

5.Properties of Exception Handling in C++

Property 1

There is a special catch block called the ‘catch-all’ block,


written as catch(…), that can be used to catch all types of
exceptions.
Example
In the following program, an int is thrown as an exception, but there
is no catch block for int, so the catch(…) block will be executed.
// C++ program to demonstate the use of catch all
// in exception handling.

#include <iostream>
using namespace std;

int main()
{
// try block
try {

// throw
throw 10;
}

// catch block
catch (char* excp) {
cout << "Caught " << excp;
}

// catch all
catch (...) {
cout << "Default Exception\n";
}
return 0;
}
Output:
Default Exception

Property 2

Implicit type conversion doesn’t happen for primitive types.


Example
In the following program, ‘a’ is not implicitly converted to int.
//// C++ program to demonstate property 2: Implicit type
/// conversion doesn't happen for primitive types.
// in exception handling.

#include <iostream>
using namespace std;

int main()
{
try {
throw 'a';
}
catch (int x) {
cout << "Caught " << x;
}
catch (...) {
cout << "Default Exception\n";
}
return 0;
}
Output:
Default Exception

Property 3

If an exception is thrown and not caught anywhere, the


program terminates abnormally.
Example
In the following program, a char is thrown, but there is no catch
block to catch the char.
// C++ program to demonstate property 3: If an exception is
// thrown and not caught anywhere, the program terminates
// abnormally in exception handling.

#include <iostream>
using namespace std;

int main()
{
try {
throw 'a';
}
catch (int x) {
cout << "Caught ";
}
return 0;
}
Output
terminate called after throwing an instance of 'char'

You might also like