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.");
}
Natija:
Error: Index was outside the bounds of the array.
Finally block executed.
Top comments (0)