Exception handling — proper error management.
Catch specific exceptions:
1// Good2catch (HttpRequestException ex)3{4 Console.WriteLine($"HTTP error: {ex.Message}");5}67// Bad — catches everything8catch (Exception ex)9{10 Console.WriteLine($"Error: {ex.Message}");11}
Exception filters:
1catch (Exception ex) when (ex.Message.Contains("timeout"))2{3 Console.WriteLine("Timeout error");4}
Custom exceptions:
1public class OrderNotFoundException : Exception2{3 public int OrderId { get; }45 public OrderNotFoundException(int orderId)6 : base($"Order {orderId} not found")7 {8 OrderId = orderId;9 }10}
Best practices: