Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Try-catch execution flow
📖 Scenario: Imagine you are writing a simple program that divides two numbers. Sometimes, the second number might be zero, which causes an error. You want to handle this error gracefully so the program does not crash.
🎯 Goal: You will create a program that tries to divide two numbers and uses try-catch blocks to handle division by zero errors. You will see how the program flow changes when an error happens.
📋 What You'll Learn
Create two integer variables named numerator and denominator with exact values
Create a boolean variable named canDivide and set it to true
Use a try block to divide numerator by denominator and store the result in result
Use a catch block to catch DivideByZeroException and set canDivide to false
Print the value of result if division was successful, or print "Cannot divide by zero" if it was not
💡 Why This Matters
🌍 Real World
Handling errors like division by zero is important in real programs to avoid crashes and provide friendly messages to users.
💼 Career
Understanding try-catch blocks is essential for writing robust C# applications and debugging errors effectively.
Progress0 / 4 steps
1
DATA SETUP: Create numerator and denominator variables
Create two integer variables called numerator with value 10 and denominator with value 0.
C Sharp (C#)
Hint
Use int numerator = 10; and int denominator = 0; to create the variables.
2
CONFIGURATION: Create canDivide variable
Create a boolean variable called canDivide and set it to true.
C Sharp (C#)
Hint
Use bool canDivide = true; to create the variable.
3
CORE LOGIC: Use try-catch to divide and handle errors
Use a try block to divide numerator by denominator and store the result in an integer variable called result. Use a catch block to catch DivideByZeroException and set canDivide to false.
C Sharp (C#)
Hint
Put the division inside try { } and catch DivideByZeroException to set canDivide = false;.
4
OUTPUT: Print the result or error message
Use an if statement to check if canDivide is true. If yes, print "Result: " followed by result. Otherwise, print "Cannot divide by zero".
C Sharp (C#)
Hint
Use if (canDivide) { ... } else { ... } and System.Console.WriteLine to print the messages.
Practice
(1/5)
1. What happens when an exception occurs inside a try block in C#?
easy
A. The program immediately jumps to the catch block to handle the error.
B. The program ignores the error and continues running the try block.
C. The program stops running without executing any further code.
D. The program restarts the try block from the beginning.
Solution
Step 1: Understand try-catch flow
The try block runs code that might cause an error.
Step 2: Exception triggers catch block
If an error happens, control moves to the catch block to handle it.
Final Answer:
The program immediately jumps to the catch block to handle the error. -> Option A
Quick Check:
Exception in try -> catch runs [OK]
Hint: Errors in try always jump to catch block [OK]
Common Mistakes:
Thinking the program ignores errors in try
Assuming the program restarts try block
Believing the program stops without catch
2. Which of the following is the correct syntax to catch an exception in C#?
easy
A. try { /* code */ } catch (ex Exception) { /* handle */ }
B. try { /* code */ } catch Exception ex { /* handle */ }
C. try { /* code */ } catch { Exception ex } { /* handle */ }