try
{
}
catch (ExceptionType ex)
{
}
finally
{
}
try
{
int result = 10 / int.Parse("0");
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Sıfıra bölme hatası");
}
catch (FormatException ex)
{
Console.WriteLine("Format hatası");
}
catch (Exception ex)
{
Console.WriteLine("Genel hata");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
}
Console.WriteLine(ex.Source);
Console.WriteLine(ex.HResult);
}
catch (ArgumentNullException ex)
{
Console.WriteLine("Parametre null olamaz");
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine("Değer aralık dışında");
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Bu işlem şu anda yapılamaz");
}
catch (NotImplementedException ex)
{
Console.WriteLine("Bu özellik henüz uygulanmadı");
}
catch (IOException ex)
{
Console.WriteLine("Dosya işlemi başarısız");
}
catch (NullReferenceException ex)
{
Console.WriteLine("Null değere erişilmeye çalışıldı");
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Dizi indeksi geçersiz");
}
catch (Exception ex)
{
Console.WriteLine($"Hata: {ex.Message}");
File.AppendAllText("errors.log", ex.ToString());
logger.LogError(ex, "Bir hata oluştu");
}
catch (Exception ex)
{
Console.WriteLine("Hata işlendi");
throw;
}
catch (Exception ex)
{
throw new ApplicationException("Özel hata", ex);
}
catch (HttpRequestException ex) when (ex.Message.Contains("404"))
{
Console.WriteLine("Sayfa bulunamadı");
}
catch (HttpRequestException ex) when (ex.Message.Contains("500"))
{
Console.WriteLine("Sunucu hatası");
}
try
{
StreamReader reader = new StreamReader("file.txt");
string content = reader.ReadToEnd();
}
catch (FileNotFoundException ex)
{
Console.WriteLine("Dosya bulunamadı");
}
finally
{
reader?.Dispose();
}
try
{
using (StreamReader reader = new StreamReader("file.txt"))
{
string content = reader.ReadToEnd();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
try
{
}
catch (DivideByZeroException) { }
catch (FormatException) { }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
public class CustomException : Exception
{
public CustomException(string message) : base(message) { }
}
try
{
throw new CustomException("Özel hata");
}
catch (CustomException ex)
{
Console.WriteLine(ex.Message);
}
- Spesifik Exception yakala: Genel
Exception yerine spesifik türler kullan - Finally kullan: Kaynakları temizlemek için
- Loglama yap: Hataları kayıt et
- Anlamlı mesajlar: Kullanıcıya açık hata mesajları ver
- Hata yeniden fırlat: Gerekirse hatayı üst seviyeye gönder