📖 Scenario: Imagine you are building a simple calculator 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 so the program does not crash.
🎯 Goal: You will create a program that uses a try-catch block to catch division by zero errors and display a friendly message instead of crashing.
📋 What You'll Learn
Create two integer variables named numerator and denominator with exact values 10 and 0 respectively.
Create a boolean variable named error_occurred and set it to false.
Use a try block to perform the division numerator / denominator and store the result in an integer variable named result.
Use a catch block to catch a std::exception and set error_occurred to true.
Print "Error: Division by zero is not allowed." if error_occurred is true, otherwise print the division result.
💡 Why This Matters
🌍 Real World
Try-catch blocks are used in real programs to handle unexpected errors like file not found, network problems, or invalid user input without crashing.
💼 Career
Understanding error handling is important for writing reliable software and is a common requirement in programming jobs.
Progress0 / 4 steps
1
Create variables for division
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
Add error flag variable
Create a boolean variable called error_occurred and set it to false.
C++
Hint
Use bool error_occurred = false; to create the flag variable.
3
Use try-catch block for division
Write a try block that divides numerator by denominator and stores the result in an integer variable called result. Then write a catch block that catches std::exception& and sets error_occurred to true.
C++
Hint
Use try { ... } and catch (const std::exception& e) { ... }. Throw an exception if denominator is zero.
4
Print the result or error message
Write an if statement that checks if error_occurred is true. If yes, print "Error: Division by zero is not allowed.". Otherwise, print the division result stored in result. Note: Declare result before the try block so it is accessible.
C++
Hint
Use if (error_occurred) { std::cout << ... } else print the result.
Practice
(1/5)
1.
What is the main purpose of a try-catch block in C++?
easy
A. To handle errors and exceptions safely during program execution
B. To speed up the program by skipping code
C. To declare variables globally
D. To create loops that run multiple times
Solution
Step 1: Understand the role of try block
The try block contains code that might cause an error or exception.
Step 2: Understand the role of catch block
The catch block handles the error so the program does not crash.
Final Answer:
To handle errors and exceptions safely during program execution -> Option A
Quick Check:
Try-catch handles errors = B [OK]
Hint: Try-catch blocks catch errors to keep programs running [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 start a try block in C++?
?
easy
A. catch {
B. try (
C. try:
D. try {
Solution
Step 1: Recall try block syntax
The try block starts with the keyword try followed by curly braces { }.
Step 2: Identify correct option
Only try { uses try { which is correct syntax to start the block.
Final Answer:
try { -> Option D
Quick Check:
Try block starts with try { = C [OK]
Hint: Try block always starts with 'try {' in C++ [OK]
Common Mistakes:
Using parentheses instead of curly braces
Starting with catch instead of try
Using colon after try
3.
What will be the output of the following C++ code?
try {
int x = 5 / 0;
std::cout << "Result: " << x << std::endl;
} catch (const std::exception& e) {
std::cout << "Caught exception" << std::endl;
}
medium
A. Result: 0
B. Caught exception
C. Program crashes with division by zero error
D. No output
Solution
Step 1: Analyze division by zero in C++
Integer division by zero causes undefined behavior and usually crashes the program; it does not throw a standard exception.
Step 2: Check catch block type
The catch block catches std::exception types, but division by zero does not throw this exception automatically.
Final Answer:
Program crashes with division by zero error -> Option C
Quick Check:
Integer division by zero crashes = D [OK]
Hint: Division by zero crashes, not caught by std::exception [OK]