0% found this document useful (0 votes)
2 views5 pages

Lab Assignment 6 (Exception Handling)

Exception handling in C++ is a mechanism for managing runtime errors using the keywords try, catch, and throw. It allows programs to handle unexpected situations gracefully, separating error handling from regular code and enabling error propagation. Best practices include using specific exceptions and ensuring resource cleanup during exceptions.

Uploaded by

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

Lab Assignment 6 (Exception Handling)

Exception handling in C++ is a mechanism for managing runtime errors using the keywords try, catch, and throw. It allows programs to handle unexpected situations gracefully, separating error handling from regular code and enabling error propagation. Best practices include using specific exceptions and ensuring resource cleanup during exceptions.

Uploaded by

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

Exception Handling in C++

Exception handling in C++ is a mechanism that allows a program to deal with unexpected situations or
errors during runtime, without terminating abruptly. This feature provides a robust way to ensure
program stability and is implemented using three primary keywords: try, catch, and throw.

Key Concepts of Exception Handling:

1. Throwing an Exception:

When an error or unexpected situation occurs, the program "throws" an exception using the throw
keyword.
Example:
if (denominator == 0) {
throw "Division by zero!";
}

2. Catching an Exception:

After an exception is thrown, the program transfers control to a catch block that matches the type of the
exception.
Example:
catch (const char* msg) {
cout << msg << endl;
}

3. Try Block:

A try block contains the code that might generate exceptions.


Example:
try {
// Code that might throw an exception
}

Advantages of Exception Handling

Separates error handling code from regular code.


Provides a mechanism to propagate errors.
Enables grouping and categorization of error types.
Best Practices
Use exceptions for error conditions, not regular control flow.
Always clean up resources (e.g., memory) in the case of an exception.
Use specific exception types instead of generic ones for better clarity.
Lab Questions

Question 1: Basic Exception Handling


Write a C++ program to divide two numbers. Throw an exception if the denominator is zero.
Answer:
Question 2: Handling Multiple Exceptions
Write a program that throws and catches multiple types of exceptions (e.g., integer, float, string).
Answer:
Question 3: Negative Input Handling
Write a C++ program to compute the square root of a number. Throw an exception if the input is
negative.
Answer:

Question 4: Array Index Validation


Write a C++ program to access an array element. Throw an exception if the index is out of
bounds.
Answer:
Question 5: Divide by Zero in a Loop
Write a C++ program to perform division repeatedly in a loop. Stop the loop if the user enters
zero as the denominator.
Answer:

You might also like