C# 0 The Complete Reference


PART I C h a p t e r   1 4



Download 4,07 Mb.
Pdf ko'rish
bet484/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   480   481   482   483   484   485   486   487   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 4 :  

U s i n g   I / O  

373


PART IPART I

The exceptions 



PathTooLongException

,

DirectoryNotFoundException

, and 

FileNotFoundException

 are subclasses of 



IOException

. Thus, it is possible to catch 

all three by catching 

IOException

.

The following shows one way to open the file 



test.dat

 for input:

FileStream fin;

try {


  fin = new FileStream("test", FileMode.Open);

}

catch(IOException exc) { // catch all I/O exceptions



  Console.WriteLine(exc.Message);

  // Handle the error.

}

catch(Exception exc { // catch any other exception.



  Console.WriteLine(exc.Message);

  // Handle the error.

}

Here, the first 



catch

 clause handles situations in which the file is not found, the path is too 

long, the directory does not exist, or other I/O errors occur. The second 

catch

, which is a 

“catch all” clause for all other types of exceptions, handles the other possible errors (possibly 

by rethrowing the exception). You could also check for each error individually, reporting 

more specifically the problem that occurred and taking remedial action specific to that error.

For the sake of simplicity, the examples in this book will catch only 



IOException

, but 


your real-world code may (probably will) need to handle the other possible exceptions, 

depending upon the circumstances. Also, the exception handlers in this chapter simply 

report the error, but in many cases, your code should take steps to correct the problem when 

possible. For example, you might reprompt the user for a filename if the one previously 

entered is not found.


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   480   481   482   483   484   485   486   487   ...   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