12. Exception Handling

sunnat_qayumov

Sunnat Qayumov

Posted on October 14, 2024

12. Exception Handling

a)try, catch, finally bloklari qanday ishlaydi?

C# dasturlash tilida try, catch, va finally bloklari istisno (exception) holatlarini boshqarish uchun ishlatiladi. Ushbu bloklar kodni xatolardan himoya qilishga yordam beradi.

  1. try bloki:

    try
    {
        int number = int.Parse("not_a_number");
    }
    
  2. catch bloki:

    catch (FormatException ex)
    {
        Console.WriteLine("Xato: noto'g'ri format.");
    }
    
  3. finally bloki:

    finally
    {
        Console.WriteLine("Finally bloki bajarildi.");
    }
    

b) Quyidagi kod nima qiladi?

try
{
    int[] numbers = { 1, 2, 3 };
    Console. WriteLine (numbers [10]);
}
catch (IndexOutOfRangeException e)
{
    Console. WriteLine("Error: " + e.Message);
}
finally
{
    Console. WriteLine("Finally block executed.");
}
Enter fullscreen mode Exit fullscreen mode

Javob: try bloki ichida, numbers[10] ifodasi xatoga
olib keladi, chunki numbers massivining
faqat 0, 1 va 2 indekslari mavjud.

💖 💪 🙅 🚩
sunnat_qayumov
Sunnat Qayumov

Posted on October 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related