Try-catch helps your program handle errors without crashing. It lets you catch problems and decide what to do next.
When reading a file that might not exist.
When dividing numbers and the divisor could be zero.
When connecting to the internet and the connection might fail.
When converting user input to a number that might be invalid.
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
This catches a divide by zero error and prints a message.
C Sharp (C#)
try
{
int x = 10 / 0;
}
catch (DivideByZeroException e)
{
Console.WriteLine("Cannot divide by zero.");
}
This catches an error when trying to use a null object.
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
{
staticvoid 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");
}
}
OutputSuccess
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/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 */ }