1] What are the components of Exception Handling?
Exception handling is a critical aspect of robust and reliable programming, providing
a structured way to manage and respond to errors or exceptional conditions that
occur during the execution of a program. Here are the primary components of
exception handling:
1. try Block
• Purpose: To enclose the code that might throw an exception.
• Syntax:
try {
// Code that might throw an exception
}
2. catch Block
• Purpose: To catch and handle the exceptions thrown by the try block.
• Syntax:
catch (exceptionType e) {
// Code to handle the exception
}
• You can have multiple catch blocks to handle different types of exceptions.
3. throw Statement
• Purpose: To throw an exception when a problem occurs.
• Syntax:
throw exception;
2] Demonstrate overloading function template with suitable code in C++.
#include <iostream>
using namespace std;
// Template function to find the maximum of two numbers
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
// Overloaded template function to find the maximum of three numbers
template <typename T>
T max(T a, T b, T c) {
return max(max(a, b), c);
}
int main() {
cout << "Max of 3 and 7: " << max(3, 7) << endl; // Calls max(T, T)
cout << "Max of 3.5 and 7.1: " << max(3.5, 7.1) << endl; // Calls max(T, T)
cout << "Max of 3, 7, and 5: " << max(3, 7, 5) << endl; // Calls max(T, T, T)
return 0;
}
Explanation:
1. Two-Parameter Template Function:
o template <typename T> T max(T a, T b): Finds the maximum of two
numbers.
2. Three-Parameter Overloaded Template Function:
o template <typename T> T max(T a, T b, T c): Finds the maximum of
three numbers by utilizing the two-parameter function.
Max of 3 and 7: 7
Max of 3.5 and 7.1: 7.1
Max of 3, 7, and 5: 7
3] Explain exception handling mechanism in C++? Write a program in C++ to handle
“divide by zero” exception.Explain different types of type conversion
Exception handling in C++ provides a way to detect and manage runtime errors. The
primary components of exception handling are try, catch, and throw.
• try block: Contains the code that might throw an exception.
• catch block: Contains the code that handles the exception.
• throw statement: Used to throw an exception.
#include <iostream>
using namespace std;
void divide(int a, int b) {
if (b == 0) {
throw runtime_error("Division by zero error!");
}
cout << "Result: " << (a / b) << endl;
}
int main() {
int x = 10, y = 0;
try {
divide(x, y);
} catch (const runtime_error& e) {
cerr << "Exception: " << e.what() << endl;
}
return 0;
}
Explanation:
• try block: Encapsulates the divide(x, y) function call, which might throw an
exception.
• throw statement: Throws a runtime_error if the denominator is zero.
• catch block: Catches the exception and prints an error message.
4] Write a short note on typename and export keyword in C++.
typename Keyword
The typename keyword in C++ is primarily used in template programming to
specify that a dependent name is a type. It helps the compiler distinguish
between types and variables in template code, which enhances code clarity
and correctness.
template <typename T>
class MyClass {
typename T::value_type value; // Ensures that value_type is a type within T
};
export Keyword
The export keyword was introduced in C++ to allow template definitions to be
separated from their declarations, enabling templates to be defined in a
source file (.cpp) rather than in a header file (.h). However, it was not widely
adopted and was removed from the C++11 standard due to lack of compiler
support and complexity.
// mytemplate.h
template <typename T>
class MyTemplate {
void foo();
};
export template <typename T>
void MyTemplate<T>::foo() {
// Implementation
}