Try-catch helps your program handle errors without crashing. It lets you catch problems and decide what to do next.
Try-catch execution flow in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
try { // code that might cause an error } catch (Exception e) { // code to handle the error }
The try block contains code that might cause an error.
The catch block runs only if an error happens in the try block.
Examples
C Sharp (C#)
try { int x = 10 / 0; } catch (DivideByZeroException e) { Console.WriteLine("Cannot divide by zero."); }
C Sharp (C#)
try { string s = null; Console.WriteLine(s.Length); } catch (NullReferenceException e) { Console.WriteLine("Object was null."); }
Sample Program
This program tries to divide 5 by 0, which causes an error. The catch block catches it and prints a friendly message. The program then continues to run.
C Sharp (C#)
using System; class Program { static void Main() { try { Console.WriteLine("Start"); int a = 5; int b = 0; int c = a / b; // This causes an error Console.WriteLine("Result: " + c); } catch (DivideByZeroException) { Console.WriteLine("Oops! You tried to divide by zero."); } Console.WriteLine("End"); } }
Important Notes
If no error happens, the catch block is skipped.
You can have multiple catch blocks for different error types.
Use try-catch to keep your program running smoothly even when errors happen.
Summary
Try-catch lets you handle errors without stopping your program.
Code inside try runs first; if an error happens, catch runs.
This helps make your programs more reliable and user-friendly.
Practice
1. What happens when an exception occurs inside a
try block in C#?easy
Solution
Step 1: Understand try-catch flow
Thetryblock runs code that might cause an error.Step 2: Exception triggers catch block
If an error happens, control moves to thecatchblock to handle it.Final Answer:
The program immediately jumps to thecatchblock to handle the error. -> Option AQuick 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
Solution
Step 1: Recall correct catch syntax
The catch block must have parentheses with exception type and variable:catch (Exception ex).Step 2: Identify correct option
Only try { /* code */ } catch (Exception ex) { /* handle */ } uses the correct syntax with parentheses and exception variable.Final Answer:
try { /* code */ } catch (Exception ex) { /* handle */ } -> Option DQuick Check:
Correct catch syntax = catch (Exception ex) [OK]
Hint: Catch needs parentheses with exception type and variable [OK]
Common Mistakes:
- Omitting parentheses around exception
- Swapping exception type and variable order
- Using braces instead of parentheses
3. What will be the output of this C# code?
try {
Console.WriteLine("Start");
int x = 5 / 0;
Console.WriteLine("End");
} catch (DivideByZeroException) {
Console.WriteLine("Error caught");
}medium
Solution
Step 1: Trace code inside try block
"Start" prints first. Then division by zero causes an exception.Step 2: Exception triggers catch block
Catch block runs and prints "Error caught". The line after division is skipped.Final Answer:
Start\nError caught -> Option AQuick Check:
Exception skips rest of try, catch prints message [OK]
Hint: Exception skips rest of try, catch runs next [OK]
Common Mistakes:
- Assuming 'End' prints after exception
- Thinking catch runs before 'Start'
- Ignoring exception and continuing try
4. Identify the error in this C# code snippet:
try {
int[] arr = new int[2];
arr[3] = 10;
} catch (Exception e) {
Console.WriteLine("Exception caught");
}medium
Solution
Step 1: Analyze the try block code
Accessing index 3 in an array of size 2 causes an IndexOutOfRangeException.Step 2: Check catch block handling
Catch block catches all exceptions of type Exception, so it will handle this error and print the message.Final Answer:
The code will throw an exception but catch block handles it correctly. -> Option CQuick Check:
Exception thrown and caught properly [OK]
Hint: Catch(Exception e) catches all exceptions [OK]
Common Mistakes:
- Thinking catch syntax is wrong
- Assuming exception is not caught
- Believing try block has syntax error
5. Consider this code:
What will be the output?
try {
Console.WriteLine("A");
try {
int y = int.Parse("abc");
} catch (FormatException) {
Console.WriteLine("Format error");
}
Console.WriteLine("B");
} catch (Exception) {
Console.WriteLine("General error");
}What will be the output?
hard
Solution
Step 1: Trace outer try block
Prints "A" first, then enters inner try block.Step 2: Inner try-catch handles FormatException
Parsing "abc" causes FormatException, caught by inner catch which prints "Format error".Step 3: Continue outer try after inner catch
After inner catch, prints "B". Outer catch is not triggered.Final Answer:
A\nFormat error\nB -> Option BQuick Check:
Inner catch handles error, outer continues [OK]
Hint: Inner catch handles error, outer try continues after [OK]
Common Mistakes:
- Assuming outer catch runs instead of inner
- Thinking code stops after inner exception
- Missing that 'B' prints after inner catch
