Menu
Coddy logo textTech

Finally Block

Coddy'nin C# Journey'sinin Mantık & Akış bölümünün bir parçası — ders 27 / 66.

finally bloğu, bir istisna oluşup oluşmadığına bakılmaksızın, hatta try veya catch bloklarında bir return ifadesi olsa bile her zaman yürütülen kodu içerir.

Temel bir try-catch-finally yapısı oluşturun:

try
{
    // Code that might throw an exception
    int result = 10 / 2;
    Console.WriteLine("Result: " + result);
}
catch (DivideByZeroException ex)
{
    // Code that handles the exception
    Console.WriteLine("Cannot divide by zero: " + ex.Message);
}
finally
{
    // Code that always executes
    Console.WriteLine("This always executes");
}

finally bloğu, dosyaları veya veritabanı bağlantılarını kapatmak gibi temizlik görevleri için kullanışlıdır:

System.IO.StreamReader file = null;
try
{
    file = new System.IO.StreamReader("data.txt");
    string content = file.ReadToEnd();
    Console.WriteLine(content);
}
catch (System.IO.FileNotFoundException)
{
    Console.WriteLine("File not found.");
}
finally
{
    // Her zaman dosyayı kapat, bir istisna oluşsa bile
    if (file != null)
    {
        file.Close();
    }
}
challenge icon

Görev

Kolay

ProcessData adında bir metot oluşturun ki:

  1. Dosya adını temsil eden bir string parametresi alsın
  2. Dosyayı açmayı ve içeriğini okumayı denesin
  3. İçeriği konsola yazsın
  4. finally bloğu kullanarak kaynakları düzgün bir şekilde temizlesin
  5. FileNotFoundException ve IOException'ı yakalasın

Dosya bulunamazsa, şunu yazdırın: "ERROR: File not found: [filename]"

Başka bir IO hatası oluşursa, şunu yazdırın: "ERROR: Could not read the file: [error message]"

Her zaman, istisna oluşsa da oluşmasa da, finally bloğunda "File operation completed." yazdırın.

Kopya kağıdı

finally bloğu, bir istisna oluşup oluşmadığına bakılmaksızın her zaman yürütülen kodu içerir, hatta try veya catch bloklarında bir return ifadesi olsa bile.

Temel try-catch-finally yapısı:

try
{
    // Code that might throw an exception
    int result = 10 / 2;
    Console.WriteLine("Result: " + result);
}
catch (DivideByZeroException ex)
{
    // Code that handles the exception
    Console.WriteLine("Cannot divide by zero: " + ex.Message);
}
finally
{
    // Code that always executes
    Console.WriteLine("This always executes");
}

finally bloğu, dosyaları veya veritabanı bağlantılarını kapatmak gibi temizlik görevleri için kullanışlıdır:

System.IO.StreamReader file = null;
try
{
    file = new System.IO.StreamReader("data.txt");
    string content = file.ReadToEnd();
    Console.WriteLine(content);
}
catch (System.IO.FileNotFoundException)
{
    Console.WriteLine("File not found.");
}
finally
{
    // Always close the file, even if an exception occurred
    if (file != null)
    {
        file.Close();
    }
}

Kendin dene

using System;
using System.IO;

class Program
{
    public static void ProcessData(string filename)
    {
        // Buraya kodunuzu yazın
    }
    
    static void Main(string[] args)
    {
        string filename = Console.ReadLine();
        ProcessData(filename);
    }
}
quiz iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Mantık & Akış bölümündeki tüm dersler