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

COET 3 Group Assignment 2

The document outlines an assignment on Object-Oriented Programming in C++ focusing on exception handling. It covers various topics including the definition and purpose of exceptions, advantages of exception handling, the use of try and catch blocks, the throw statement, predefined exceptions, creating custom exception classes, and stack unwinding. Submission guidelines specify that the assignment should be submitted as a single PDF or Word document by January 21, 2025.

Uploaded by

ezekiel nyamu
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)
8 views3 pages

COET 3 Group Assignment 2

The document outlines an assignment on Object-Oriented Programming in C++ focusing on exception handling. It covers various topics including the definition and purpose of exceptions, advantages of exception handling, the use of try and catch blocks, the throw statement, predefined exceptions, creating custom exception classes, and stack unwinding. Submission guidelines specify that the assignment should be submitted as a single PDF or Word document by January 21, 2025.

Uploaded by

ezekiel nyamu
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

COET 3 Group Assignment 2:Object-Oriented Programming in C++

Instructions: Answer all the questions provided.


Marks: 5 marks

1. Introduction to Exceptions

 Definition: Explain what an exception is in the context of C++


programming.
 Purpose: Describe the role of exceptions in handling runtime errors.
 When to Use: Discuss scenarios where exception handling is useful,
such as when a program encounters unexpected situations like invalid
user input or system errors.
 Syntax Overview: Provide an overview of the basic syntax for
exception handling in C++ (try, throw, and catch blocks).
 Explain with an example of a simple program that uses exception
handling to deal with division by zero.

2. Advantages of Exception Handling

 Code Structure: Discuss how exception handling improves code


structure by separating error-handling logic from the main program
logic.
 Error Propagation: Explain how exceptions allow errors to be
propagated up the call stack, enabling more efficient error handling.
 Maintainability: Describe how using exceptions can make programs
easier to maintain and modify.
 Clarity: Discuss how exception handling makes the code more readable
by eliminating the need for complex error-checking logic.
 Compare a program that uses traditional error-checking methods (such
as returning error codes) with one that uses exception handling. Show
the difference in code clarity and structure.

3. The Try and Catch Blocks

 Try Block: Explain the purpose of the try block and what types of code it
should contain (e.g., code that might throw exceptions).
 Catch Block: Describe the catch block and how it is used to handle
exceptions that occur in the try block.
 Multiple Catch Blocks: Explain how multiple catch blocks can be used
to handle different types of exceptions.
 Exception Matching: Discuss how C++ matches the thrown exception
to the appropriate catch block.
 Provide an example that demonstrates using multiple catch blocks to
handle different types of exceptions.

4. The Throw Statement

 Syntax and Usage: Explain the throw statement and how it is used to
throw exceptions in C++.
 Types of Objects: Discuss the different types of objects that can be
thrown (such as integers, strings, and user-defined objects).
 Exception Propagation: Explain how exceptions are passed from the
try block to the catch block using throw.
 Provide an example where a function throws an exception if an invalid
input is provided.

5. Predefined Exceptions in C++

 Standard Exception Hierarchy: Discuss some of the predefined


exceptions available in C++ (such as std::exception, std::runtime_error,
std::out_of_range, and std::invalid_argument).
 Use Cases: Explain when and why you would use these predefined
exceptions instead of custom ones.
 What() Function: Describe how the what() function is used to retrieve a
message describing the exception.
 Provide an example using std::runtime_error and std::out_of_range
exceptions.

6. Creating Custom Exception Classes

 Custom Exception Class: Explain how to create custom exception


classes by inheriting from std::exception or another base class.
 Overriding what() Function: Show how to override the what() function
in custom exception classes to provide specific error messages.
 Use Cases for Custom Exceptions: Discuss when it is necessary to
create custom exceptions (e.g., domain-specific errors).
 Provide an example of a custom exception class NegativeValueException
and how it is used.

7. Stack Unwinding

 Definition: Explain what stack unwinding is and how it works in C++


when an exception is thrown.
 How Stack Unwinding Occurs: Discuss how local variables (such as
objects) are destroyed during stack unwinding, which helps in cleaning
up resources.
 Destructor Calls: Explain how destructors are called automatically for
objects as part of stack unwinding.
 Provide an example demonstrating stack unwinding with objects and
destructor calls.

Submission Guidelines:

 Submit the assignment in a single PDF or Word document.


 Include detailed explanations, code examples, and relevant comments.
 Ensure the code is well-commented and formatted.
 Submit the assignment by 21 January 2025 via [email protected]
 Max pages is 15

You might also like