C# 0 The Complete Reference


try  block is  put in a  finally



Download 4,07 Mb.
Pdf ko'rish
bet437/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   433   434   435   436   437   438   439   440   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

try

 block is 

put in a 

finally

 block.


Using try and catch

At the core of exception handling are 



try

 and 


catch

. These keywords work together, and you 

can’t have a 

catch

 without a 



try

. Here is the general form of the 



try

/

catch

 exception-handling 

blocks:


try {

   // block of code to monitor for errors

}

catch (


ExcepType1 exOb

) {


   // handler for 

ExcepType1

}

catch (



ExcepType2 exOb

) {


   // handler for 

ExcepType2

}

.



.

.

Here, 



ExcepType

 is the type of exception that has occurred. When an exception is thrown, 

it is caught by its corresponding 

catch

 clause, which then processes the exception. As the 

general form shows, more than one 

catch

 clause can be associated with a 



try

. The type of 

the exception determines which 

catch

 is executed. That is, if the exception type specified 

by a 

catch

 matches that of the exception, then that 



catch

 is executed (and all others are 

bypassed). When an exception is caught, the exception variable 

exOb

 will receive its value.

Actually, specifying 

exOb

 is optional. If the exception handler does not need access to the 

exception object (as is often the case), there is no need to specify 

exOb.

 The exception type 

alone is sufficient. For this reason, many of the examples in this chapter will not specify 

exOb.

Here is an important point: If no exception is thrown, then a 



try

 block ends normally, 

and all of its 

catch

 clauses are bypassed. Execution resumes with the first statement 

following the last 

catch

. Thus, a 



catch

 is executed only if an exception is thrown.



A Simple Exception Example

Here is a simple example that illustrates how to watch for and catch an exception. As you 

know, it is an error to attempt to index an array beyond its boundaries. When this error 

occurs, the CLR throws an 



IndexOutOfRangeException

, which is a standard exception 

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   433   434   435   436   437   438   439   440   ...   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