Bird
Raised Fist0
C++programming~30 mins

Exception handling flow in C++ - Mini Project: Build & Apply

Choose your learning style10 modes available
Exception handling flow
๐Ÿ“– Scenario: Imagine you are writing a simple program that divides two numbers. Sometimes, the user might enter zero as the divisor, which causes an error. We want to handle this error gracefully using exception handling.
๐ŸŽฏ Goal: You will create a program that safely divides two numbers by using exception handling flow in C++. You will learn how to throw and catch exceptions to avoid program crashes.
๐Ÿ“‹ What You'll Learn
Create two integer variables with exact values
Create a function that divides two integers and throws an exception if divisor is zero
Use try-catch blocks to handle the exception
Print the division result or an error message
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Exception handling is used in real programs to prevent crashes and handle errors like dividing by zero or file not found.
๐Ÿ’ผ Career
Understanding exception handling is essential for writing robust C++ applications in software development jobs.
Progress0 / 4 steps
1
DATA SETUP: Create two integer variables
Create two integer variables called numerator and denominator with values 10 and 0 respectively.
C++
Hint

Use int numerator = 10; and int denominator = 0; to create the variables.

2
CONFIGURATION: Create a division function with exception
Write a function called divide that takes two integers a and b. If b is zero, throw a std::runtime_error with the message "Division by zero". Otherwise, return the result of a / b.
C++
Hint

Use throw std::runtime_error("Division by zero") inside the function when b is zero.

3
CORE LOGIC: Use try-catch to handle the exception
Write a try block that calls divide(numerator, denominator) and stores the result in an integer variable called result. Then write a catch block that catches std::runtime_error exceptions and stores the exception message in a variable called error_message.
C++
Hint

Use try and catch (const std::runtime_error& e) to handle the exception.

4
OUTPUT: Print the result or error message
Write code to print "Result: " followed by result if no exception occurred. Otherwise, print "Error: " followed by error_message.
C++
Hint

Use std::cout to print the result or error message depending on whether error_message is empty.

Practice

(1/5)
1. What is the main purpose of using try and catch blocks in C++?
easy
A. To speed up the program execution
B. To handle errors and keep the program running smoothly
C. To declare variables
D. To create loops

Solution

  1. Step 1: Understand the role of try block

    The try block contains code that might cause errors or exceptions during execution.
  2. Step 2: Understand the role of catch block

    The catch block handles those errors so the program does not crash and can continue running.
  3. Final Answer:

    To handle errors and keep the program running smoothly -> Option B
  4. Quick Check:

    Exception handling = handle errors safely [OK]
Hint: Try-catch blocks catch errors to avoid crashes [OK]
Common Mistakes:
  • Thinking try-catch speeds up code
  • Confusing try-catch with loops
  • Using try-catch to declare variables
2. Which of the following is the correct syntax to catch an exception of type int in C++?
easy
A. catch (int e) { /* handle error */ }
B. catch int (e) { /* handle error */ }
C. catch { int e; /* handle error */ }
D. catch (e int) { /* handle error */ }

Solution

  1. Step 1: Recall correct catch syntax

    The catch block must specify the exception type in parentheses followed by a variable name, like catch (int e).
  2. 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.
  3. Final Answer:

    catch (int e) { /* handle error */ } -> Option A
  4. Quick Check:

    Correct catch syntax = catch (type var) [OK]
Hint: Catch syntax: catch (type variable) { } [OK]
Common Mistakes:
  • Omitting parentheses in catch
  • Swapping type and variable order
  • Missing variable name in catch
3. What will be the output of the following code?
try {
  throw 5;
  std::cout << "No Exception" << std::endl;
} catch (int e) {
  std::cout << "Caught exception: " << e << std::endl;
}
medium
A. Program crashes
B. No Exception
C. Caught exception: 5
D. No output

Solution

  1. Step 1: Understand the throw statement

    The code throws an integer exception with value 5, so control jumps to the catch block immediately.
  2. Step 2: Identify which catch block runs

    The catch block for int e catches the exception and prints the message with the value 5.
  3. Final Answer:

    Caught exception: 5 -> Option C
  4. Quick Check:

    Throw 5 caught by catch(int) = output message [OK]
Hint: Throw jumps to matching catch, skipping rest of try [OK]
Common Mistakes:
  • Expecting code after throw to run
  • Thinking program crashes without catch
  • Ignoring exception value in output
4. Identify the error in this code snippet:
try {
  int x = 10 / 0;
} catch (double e) {
  std::cout << "Caught double exception" << std::endl;
}
medium
A. Division by zero causes a runtime error but no exception is thrown
B. Catch block should catch int, not double
C. Try block syntax is incorrect
D. No error, code runs fine

Solution

  1. 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.
  2. Step 2: Check catch block type

    The catch block expects a double exception, but no exception is thrown, so it never runs.
  3. Final Answer:

    Division by zero causes a runtime error but no exception is thrown -> Option A
  4. Quick Check:

    Integer divide by zero = runtime error, no catch [OK]
Hint: Integer divide by zero crashes, no catch thrown [OK]
Common Mistakes:
  • Assuming division by zero throws exception
  • Mismatching catch type with thrown exception
  • Thinking try block syntax is wrong
5. Given this code, what will be the output?
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;
}
hard
A. Caught int
B. Program crashes
C. No output
D. Caught string: Error

Solution

  1. Step 1: Trace inner try-catch

    The inner try throws a std::string exception. The inner catch expects int, so it does not catch this exception.
  2. Step 2: Trace outer catch block

    The exception propagates to the outer catch, which catches std::string &e and prints the message.
  3. Final Answer:

    Caught string: Error -> Option D
  4. Quick Check:

    Inner catch mismatch, outer catch handles string [OK]
Hint: Exception bubbles up until matching catch found [OK]
Common Mistakes:
  • Assuming inner catch catches all exceptions
  • Ignoring exception propagation
  • Expecting program crash on uncaught inner exception
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard