0
0
C++programming~20 mins

Why exception handling is required in C++ - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Exception Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use exception handling in C++?

Which of the following best explains why exception handling is important in C++?

AIt speeds up the program execution by skipping error checks.
BIt automatically fixes all bugs in the program without programmer intervention.
CIt prevents the program from using any memory during runtime.
DIt helps manage errors by separating error-handling code from regular code, making programs easier to read and maintain.
Attempts:
2 left
πŸ’‘ Hint

Think about how handling errors separately can improve code clarity and reliability.

❓ Predict Output
intermediate
2:00remaining
Output of code with exception handling

What is the output of this C++ code?

C++
#include <iostream>
using namespace std;

int main() {
    try {
        throw 5;
        cout << "After throw" << endl;
    } catch (int e) {
        cout << "Caught exception: " << e << endl;
    }
    cout << "Program continues" << endl;
    return 0;
}
A
Caught exception: 5
Program continues
B
After throw
Caught exception: 5
Program continues
C
Program continues
Caught exception: 5
D
Caught exception: 0
Program continues
Attempts:
2 left
πŸ’‘ Hint

Remember that code after throw inside try is not executed.

❓ Predict Output
advanced
2:00remaining
What error occurs without exception handling?

What happens when this code runs without any exception handling?

C++
#include <iostream>
using namespace std;

int divide(int a, int b) {
    return a / b;
}

int main() {
    int x = 10, y = 0;
    cout << divide(x, y) << endl;
    return 0;
}
AOutput: 0
BRuntime error: division by zero (undefined behavior or crash)
CCompile-time error: division by zero
DOutput: 10
Attempts:
2 left
πŸ’‘ Hint

Think about what happens when you divide by zero in C++ without protection.

🧠 Conceptual
advanced
2:00remaining
Benefits of exception handling over error codes

Which statement correctly describes a benefit of using exception handling instead of error codes?

AError codes are easier to maintain because they never require special syntax.
BException handling makes the program run faster by skipping all error checks.
CException handling allows errors to be caught far from where they occur, avoiding cluttering code with checks everywhere.
DException handling forces the program to stop immediately on any error.
Attempts:
2 left
πŸ’‘ Hint

Consider how exception handling separates error logic from normal logic.

πŸš€ Application
expert
3:00remaining
Identify the output with nested try-catch blocks

What is the output of this C++ program?

C++
#include <iostream>
using namespace std;

int main() {
    try {
        try {
            throw "Error inside inner try";
        } catch (const char* msg) {
            cout << "Inner catch: " << msg << endl;
            throw;
        }
    } catch (const char* msg) {
        cout << "Outer catch: " << msg << endl;
    }
    cout << "Program ends" << endl;
    return 0;
}
A
Inner catch: Error inside inner try
Outer catch: Error inside inner try
Program ends
B
Inner catch: Error inside inner try
Program ends
C
Outer catch: Error inside inner try
Program ends
DProgram ends
Attempts:
2 left
πŸ’‘ Hint

Notice the throw; inside the inner catch rethrows the exception.