๐ 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
Step 1: Understand the role of try block
The try block contains code that might cause errors or exceptions during execution.
Step 2: Understand the role of catch block
The catch block 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 B
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
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).
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.