CSE TUTORIAL - 3
Submitted by- Arsh Mevati 23MIP10040
1. Differentiate between errors anD exCeptions.
Ans: Errors occur during both compilation (compile-time errors) and
execution (run-time errors) and can include syntax errors, logical errors, and
other issues that prevent the program from running correctly. Exceptions,
on the other hand, are specific issues that arise during program execution
due to abnormal conditions, such as file not found, division by zero, or
network errors. Exceptions are used for handling these exceptional
situations gracefully in the code.
2. what are the Causes of exCeptions in C++?
Ans: Exceptions in C++ can be caused by various factors, including:
1. Attempting to access invalid memory locations (e.g., null pointer
dereference).
2. Division by zero.
3. Trying to open or access files that do not exist.
4. Index out of bounds errors in arrays or other data structures.
5. Resource allocation failures (e.g., running out of memory).
6. Invalid input or unexpected conditions in program logic.
7. Errors in external operations such as network communication or database
access.
3. Explain in short the types of exception with examples.
Ans: In C++, exceptions are typically categorized into three main types based on
their origin and usage:
1. Standard exceptions:
• These exceptions are predefined in the C++ Standard Library and
are commonly used for general-purpose error handling.
• Example: std::invalid_argument is thrown when a function
argument is invalid.
2. Custom exceptions:
• These are user-defined exceptions created by deriving from
std::exception or its subclasses.
• Example: Creating a custom exception class MyException that
inherits from std::exception to handle specific errors in a
program.
3. System exceptions:
• These exceptions are related to system-level errors or external
operations such as file I/O, networking, or memory management.
• Example: std::runtime_error is thrown when a runtime error
occurs, such as failure to open a file.
4. explain the meChanism of exCeption hanDling.
Ans: Throwing: Use throw to raise an exception when an error occurs.
• Try-Catch: Enclose code that might throw exceptions in a try block,
followed by one or more catch blocks to handle specific exceptions.
• Handling: In a catch block, handle the exception by providing appropriate
error handling code.
• Propagation: If an exception is not caught in a function, it propagates up
the call stack until caught or terminates the program if unhandled.
• Cleanup: Use RAII and smart pointers for resource cleanup; C++ doesn't
have a finally block like some other languages.
5. What is the need of multiple Catch blocks in Exception handling?
Ans: Multiple catch blocks in exception handling allow for:
• Specific handling of different types of exceptions.
• Hierarchical organization for catching exceptions at appropriate levels.
• Customized error logging and reporting.
• Graceful degradation of the program in the face of errors, enhancing
user experience and preventing crashes.