VuePressVuePress
Home
Get Started
Home
Get Started
  • Temel Kavramlar

    • Type Declarations (Tur Bildirimleri)
    • Access Modifiers (Erişim Değiştiricileri)
    • Erişimci (Accessor)
  • Metotlar ve İşlemler

    • Metotlar (Methods)
    • Geri Dönüş Türleri (Return Types)
    • Try-Catch Kullanımı
  • Veri Yapıları

    • Koleksiyon Arayüzleri (Collection Interfaces)
    • LINQ Sorguları
  • İleri Konular

    • Dependency Injection Servisleri
    • DbContext - OnModelCreating Metodu
  • DbContext ve Entity Framework

    • Entity Framework Core - Fluent API ve İlişkiler Rehberi

Try-Catch Kullanımı

Temel Yapı

try
{
    // Hata oluşabilecek kod
}
catch (ExceptionType ex)
{
    // Hata işleme
}
finally
{
    // Her durumda çalışacak kod
}

Catch Bloğunda Kullanılabilecek Şeyler

1. Exception Türü Belirtme

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");
}

2. Exception Özellikleri

catch (Exception ex)
{
    // Message - Hata mesajı
    Console.WriteLine(ex.Message);
    
    // StackTrace - Hata yığını
    Console.WriteLine(ex.StackTrace);
    
    // InnerException - İç hata
    if (ex.InnerException != null)
    {
        Console.WriteLine(ex.InnerException.Message);
    }
    
    // Source - Hata kaynağı
    Console.WriteLine(ex.Source);
    
    // HResult - Hata kodu
    Console.WriteLine(ex.HResult);
}

3. Sık Kullanılan Exception Türleri

// ArgumentNullException - Null parametre
catch (ArgumentNullException ex)
{
    Console.WriteLine("Parametre null olamaz");
}

// ArgumentOutOfRangeException - Geçersiz aralık
catch (ArgumentOutOfRangeException ex)
{
    Console.WriteLine("Değer aralık dışında");
}

// InvalidOperationException - Geçersiz işlem
catch (InvalidOperationException ex)
{
    Console.WriteLine("Bu işlem şu anda yapılamaz");
}

// NotImplementedException - Henüz uygulanmadı
catch (NotImplementedException ex)
{
    Console.WriteLine("Bu özellik henüz uygulanmadı");
}

// IOException - Dosya/IO hatası
catch (IOException ex)
{
    Console.WriteLine("Dosya işlemi başarısız");
}

// NullReferenceException - Null referans
catch (NullReferenceException ex)
{
    Console.WriteLine("Null değere erişilmeye çalışıldı");
}

// IndexOutOfRangeException - Dizi indeksi dışında
catch (IndexOutOfRangeException ex)
{
    Console.WriteLine("Dizi indeksi geçersiz");
}

4. Hata Loglama

catch (Exception ex)
{
    // Konsola yazma
    Console.WriteLine($"Hata: {ex.Message}");
    
    // Dosyaya yazma
    File.AppendAllText("errors.log", ex.ToString());
    
    // Logger kullanma
    logger.LogError(ex, "Bir hata oluştu");
}

5. Hata Yeniden Fırlatma

catch (Exception ex)
{
    Console.WriteLine("Hata işlendi");
    throw; // Aynı hatayı yeniden fırlat
}

catch (Exception ex)
{
    throw new ApplicationException("Özel hata", ex); // Yeni hata fırlat
}

6. Koşullu Catch (C# 6.0+)

catch (HttpRequestException ex) when (ex.Message.Contains("404"))
{
    Console.WriteLine("Sayfa bulunamadı");
}
catch (HttpRequestException ex) when (ex.Message.Contains("500"))
{
    Console.WriteLine("Sunucu hatası");
}

7. Finally Bloğu

try
{
    // Dosya aç
    StreamReader reader = new StreamReader("file.txt");
    string content = reader.ReadToEnd();
}
catch (FileNotFoundException ex)
{
    Console.WriteLine("Dosya bulunamadı");
}
finally
{
    // Her durumda çalışır - kaynakları temizle
    reader?.Dispose();
}

8. Using Statement (Otomatik Kaynakları Temizle)

try
{
    using (StreamReader reader = new StreamReader("file.txt"))
    {
        string content = reader.ReadToEnd();
    } // Otomatik olarak Dispose çağrılır
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

9. Çoklu Exception Yakalama

try
{
    // Kod
}
catch (DivideByZeroException) { }
catch (FormatException) { }
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

10. Custom Exception

public class CustomException : Exception
{
    public CustomException(string message) : base(message) { }
}

try
{
    throw new CustomException("Özel hata");
}
catch (CustomException ex)
{
    Console.WriteLine(ex.Message);
}

En İyi Uygulamalar

  • 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
Last Updated:: 6/7/26, 12:33 PM
Contributors: yigitumretastan
Prev
Geri Dönüş Türleri (Return Types)