DEV Community

Zahro
Zahro

Posted on

12. Exception Handling

a)try, catch, finally bloklari qanday ishlaydi?

a) try, catch, finally bloklari xatolarni tutib olish va ularni boshqarish uchun ishlatiladi. finally bloki doim bajariladi, hatto catch blok bajarilsa ham.

b) Quidagi kod nima qiladi?

Quyidagi kodda IndexOutOfRangeException xatosi yuzaga keladi, chunki massivning 10-chi elementi yo'q:

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

Natija:

Error: Index was outside the bounds of the array.
Finally block executed.

Top comments (0)