You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Modern applications must handle I/O operations (network calls, file access, database queries) without blocking. C# provides a first-class async/await model built on the Task type. This lesson also covers structured error handling with exceptions.
try
{
string content = File.ReadAllText("data.txt");
int number = int.Parse(content);
Console.WriteLine($"Result: {number}");
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"File not found: {ex.FileName}");
}
catch (FormatException ex)
{
Console.WriteLine($"Invalid format: {ex.Message}");
}
catch (Exception ex)
{
// Catch-all for unexpected exceptions
Console.WriteLine($"Unexpected error: {ex.Message}");
}
finally
{
// Always runs — cleanup code
Console.WriteLine("Cleanup complete");
}
| Exception | When |
|---|---|
Exception | Base class for all exceptions |
SystemException | Base for runtime exceptions |
ArgumentException | Invalid argument |
ArgumentNullException | Null argument |
InvalidOperationException | Operation not valid for current state |
NullReferenceException | Accessing a member on null |
IndexOutOfRangeException | Array index out of bounds |
IOException | I/O operation failed |
HttpRequestException | HTTP request failed |
TaskCanceledException | Task was cancelled |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.