Exception handling flow helps your program deal with unexpected problems without crashing. It lets you catch errors and decide what to do next.
Exception handling flow in C++
try { // code that might cause an exception } catch (const ExceptionType& e) { // code to handle the exception } catch (...) { // code to handle any other exceptions }
The try block contains code that might cause an error.
The catch blocks handle specific or any exceptions thrown in the try block.
try { int divisor = 0; if (divisor == 0) { throw std::runtime_error("Division by zero error!"); } int x = 10 / divisor; } catch (const std::exception& e) { std::cout << "Error: " << e.what() << std::endl; }
try { throw 5; } catch (int e) { std::cout << "Caught an int: " << e << std::endl; }
try { // risky code } catch (...) { std::cout << "Caught an unknown exception." << std::endl; }
This program asks the user for a number and divides 100 by it. If the user enters zero, it throws an exception and catches it to show a friendly message. The program then continues running.
#include <iostream> int main() { try { std::cout << "Enter a number to divide 100 by: "; int divisor; std::cin >> divisor; if (divisor == 0) { throw "Division by zero error!"; } int result = 100 / divisor; std::cout << "Result is: " << result << std::endl; } catch (const char* msg) { std::cout << "Exception caught: " << msg << std::endl; } std::cout << "Program continues after exception handling." << std::endl; return 0; }
Only code inside the try block can throw exceptions caught by the following catch blocks.
You can have multiple catch blocks to handle different exception types.
The catch(...) block catches any exception not caught by previous blocks.
Use try to wrap code that might cause errors.
Use catch to handle those errors and keep your program running.
Exception handling flow helps your program stay safe and user-friendly.
Practice
try and catch blocks in C++?Solution
Step 1: Understand the role of
Thetryblocktryblock contains code that might cause errors or exceptions during execution.Step 2: Understand the role of
Thecatchblockcatchblock handles those errors so the program does not crash and can continue running.Final Answer:
To handle errors and keep the program running smoothly -> Option BQuick Check:
Exception handling = handle errors safely [OK]
- Thinking try-catch speeds up code
- Confusing try-catch with loops
- Using try-catch to declare variables
int in C++?Solution
Step 1: Recall correct catch syntax
The catch block must specify the exception type in parentheses followed by a variable name, likecatch (int e).Step 2: Identify syntax errors in other options
The other options contain syntax errors such as reversed type and variable order, incorrect keyword placement, or missing parentheses with improper declaration.Final Answer:
catch (int e) { /* handle error */ } -> Option AQuick Check:
Correct catch syntax = catch (type var) [OK]
- Omitting parentheses in catch
- Swapping type and variable order
- Missing variable name in catch
try {
throw 5;
std::cout << "No Exception" << std::endl;
} catch (int e) {
std::cout << "Caught exception: " << e << std::endl;
}Solution
Step 1: Understand the throw statement
The code throws an integer exception with value 5, so control jumps to the catch block immediately.Step 2: Identify which catch block runs
The catch block forint ecatches the exception and prints the message with the value 5.Final Answer:
Caught exception: 5 -> Option CQuick Check:
Throw 5 caught by catch(int) = output message [OK]
- Expecting code after throw to run
- Thinking program crashes without catch
- Ignoring exception value in output
try {
int x = 10 / 0;
} catch (double e) {
std::cout << "Caught double exception" << std::endl;
}Solution
Step 1: Analyze division by zero in C++
Integer division by zero causes undefined behavior or runtime error but does not throw a C++ exception automatically.Step 2: Check catch block type
The catch block expects adoubleexception, but no exception is thrown, so it never runs.Final Answer:
Division by zero causes a runtime error but no exception is thrown -> Option AQuick Check:
Integer divide by zero = runtime error, no catch [OK]
- Assuming division by zero throws exception
- Mismatching catch type with thrown exception
- Thinking try block syntax is wrong
try {
try {
throw std::string("Error");
} catch (int e) {
std::cout << "Caught int" << std::endl;
}
} catch (std::string &e) {
std::cout << "Caught string: " << e << std::endl;
}Solution
Step 1: Trace inner try-catch
The inner try throws astd::stringexception. The inner catch expectsint, so it does not catch this exception.Step 2: Trace outer catch block
The exception propagates to the outer catch, which catchesstd::string &eand prints the message.Final Answer:
Caught string: Error -> Option DQuick Check:
Inner catch mismatch, outer catch handles string [OK]
- Assuming inner catch catches all exceptions
- Ignoring exception propagation
- Expecting program crash on uncaught inner exception
Ready to prove it?
Pass the test and this pattern moves unknown
