DEV Community

asilbek ibragimov
asilbek ibragimov

Posted on

2 1 1 1 2

12. Exception Handling

12. Exception Handling

a)try, catch, finally bloklari qanday ishlaydi?

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

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

try bloki:

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

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

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

Enter fullscreen mode Exit fullscreen mode

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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs