Ana Sayfa Hakkımda Dersler Blog MikrofondaOn The Mic İletişim Projeler Yakında
DerslerLessons TEMEL · C#

C# Temelleri — Try Catch C# Basics — Try Catch

8 dk okuma 8 min read · Emre Ulutabak
1
Try Catch nedir? What is try catch?

Hayatta bazen her şeyi planlarsın ama beklenmedik bir şey olur. Yolda giderken lastik patlar, plan değişir ama yolculuk tamamen bitmez.

Yazılımda da beklenmeyen hatalar olabilir. try catch, bu hataları yakalayıp programın tamamen çökmesini önlemeye yardımcı olur.

In life, sometimes you plan everything but something unexpected happens. A tire blows out on the road; the plan changes, but the journey does not completely end.

In software, unexpected errors can also happen. try catch helps catch these errors and prevents the program from crashing completely.

csharp
try
{
    int sayi = int.Parse("abc");
}
catch
{
    Console.WriteLine("Bir hata oluştu.");
}
💡
try, riskli kodu; catch ise oluşan hatayı karşılamayı temsil eder. try represents risky code; catch represents handling the resulting error.
2
Neden gereklidir? Why is it necessary?

Kullanıcı yanlış veri girebilir, dosya bulunamayabilir, beklenmedik bir dönüşüm hatası olabilir. Bu gibi durumlarda programın patlaması kötü bir deneyim oluşturur.

Try catch sayesinde hatayı yönetebilir, kullanıcıya daha kontrollü bir mesaj gösterebilirsin.

The user may enter wrong data, a file may not be found, or an unexpected conversion error may occur. In such cases, having the program crash creates a poor experience.

With try catch, you can manage the error and show the user a more controlled message.

3
Hata yakalama mantığı Error handling logic

Önce riskli kod try içine yazılır. Eğer hata oluşursa akış catch bloğuna geçer.

Hata oluşmazsa catch çalışmaz ve program normal devam eder.

First, the risky code is written inside try. If an error occurs, execution moves to the catch block.

If no error occurs, catch does not run and the program continues normally.

csharp
try
{
    int a = 10;
    int b = 0;
    Console.WriteLine(a / b);
}
catch
{
    Console.WriteLine("Sıfıra bölme hatası oluştu.");
}
💡
Catch bloğu hata olduğunda devreye girer; her durumda çalışmaz. The catch block runs when an error occurs; it does not run every time.
4
Finally rolü Role of finally

Bazen hata olsa da olmasa da çalışması gereken bir bölüm vardır. İşte bunun için finally kullanılır.

Örneğin bağlantı kapatma, temizlik yapma gibi işler finally içinde yer alabilir.

Sometimes there is a part that should run whether an error happens or not. That is what finally is for.

For example, tasks like closing a connection or performing cleanup can be placed inside finally.

csharp
try
{
    Console.WriteLine("İşlem başladı");
}
catch
{
    Console.WriteLine("Hata oluştu");
}
finally
{
    Console.WriteLine("İşlem bitti");
}
💡
Finally, hata olsa da olmasa da en sonda çalışır. Finally runs at the end whether an error happens or not.
5
Altın kurallar Golden rules

Try catch çok yararlıdır ama her şeyi sessizce yutmak için kullanılmamalıdır. Amaç hatayı gizlemek değil, kontrollü yönetmektir.

Try catch is very useful, but it should not be used to silently swallow everything. The goal is not to hide the error, but to manage it in a controlled way.

💡
Gerçekten hata çıkma ihtimali olan kodları try içine koy. Put code inside try only when it genuinely has a chance of throwing an error.
💡
Catch içinde kullanıcıya anlamlı bir mesaj vermek iyi bir alışkanlıktır. Giving the user a meaningful message inside catch is a good habit.
💡
Her hatayı yok saymak yerine anlamaya çalış. Instead of ignoring every error, try to understand it.
MİNİ QUIZ MINI QUIZ
Aşağıdakilerden hangisi doğrudur? Which of the following is correct?
catch bloğu yalnızca hata oluşursa çalışır The catch block runs only if an error occurs
try bloğu hata olduğunda silinir The try block is deleted when an error occurs
finally hiçbir zaman çalışmaz finally never runs
try catch sadece diziler için kullanılır try catch is only used for arrays