C# 0 The Complete Reference


catch  in  Main( )



Download 4,07 Mb.
Pdf ko'rish
bet441/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   437   438   439   440   441   442   443   444   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

catch

 in 


Main( )

. Understand, however, 

that if 

GenException( )

 had caught the exception, then it never would have been passed 

back to 

Main( )

.

The Consequences of an Uncaught Exception

Catching one of the standard exceptions, as the preceding program does, has a side benefit: 

It prevents abnormal program termination. When an exception is thrown, it must be caught 

by some piece of code, somewhere. In general, if your program does not catch an exception, 

it will be caught by the runtime system. The trouble is that the runtime system will report 

an error and terminate the program. For instance, in this example, the index out-of-bounds 

exception is not caught by the program:

// Let the C# runtime system handle the error.

using System;

class NotHandled {

  static void Main() {

    int[] nums = new int[4];

www.freepdf-books.com




342

 

P a r t   I :  



T h e   C #   L a n g u a g e

    Console.WriteLine("Before exception is generated.");

    // Generate an index out-of-bounds exception.

    for(int i=0; i < 10; i++) {

      nums[i] = i;

      Console.WriteLine("nums[{0}]: {1}", i, nums[i]);

    }

  }


}

When the array index error occurs, execution is halted and the following error message 

is displayed:

Unhandled Exception: System.IndexOutOfRangeException:

          Index was outside the bounds of the array.

   at NotHandled.Main()

Although such a message is useful while debugging, you would not want others to see it, to 

say the least! This is why it is important for your program to handle exceptions itself.

As mentioned earlier, the type of the exception must match the type specified in a 

catch

.

If it doesn’t, the exception won’t be caught. For example, the following program tries to 



catch an array boundary error with a 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   437   438   439   440   441   442   443   444   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish