— Scott McNealy, Sun Microsystems Chairman, President and ceo


catch(ClothingException cex)



Download 40,57 Mb.
Pdf ko'rish
bet313/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   309   310   311   312   313   314   315   316   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

catch(ClothingException cex)
TeeShirtExceptions are 
caught here, but no other 
exceptions will fit.
TeeShirtExceptions will 
never get here, but all 
other ShirtException 
subclasses are caught here.
All ClothingExceptions 
are caught here, although 
TeeShirtException and 
ShirtException will never 
get this far.
order of 
multiple 
catch blocks


you are here
4
exception 
handling
333
try {
laundry.doLaundry();
 } catch(ClothingException cex) {
// recovery from ClothingException
 } catch(LingerieException lex) {
// recovery from LingerieException
 } catch(ShirtException sex) {
// recovery from ShirtException
 }
Size matters when 
you have multiple catch 
blocks. The one with the biggest 
basket has to be on the bottom. 
Otherwise, the ones with 
smaller baskets are useless.
You can’t put bigger baskets 
above smaller baskets.
Well, you can but it won’t compile. Catch 
blocks are not like overloaded methods 
where the best match is picked. With catch 
blocks, the JVM simply starts at the fi rst one 
and works its way down until it fi nds a catch 
that’s broad enough (in other words, high 
enough on the inheritance tree) to handle 
the exception. If your fi rst catch block is 
catch(Exception ex)
, the compiler 
knows there’s no point in adding any 
others—they’ll never be reached.
Siblings can be in any order, because they 
can’t catch one another’s exceptions. 
You could put ShirtException above 
LingerieException and nobody would mind.
Because even though ShirtException is a bigger 
(broader) type because it can catch other classes 
(its own subclasses), ShirtException can’t catch a 
LingerieException so there’s no problem.
Don’t do this!



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   309   310   311   312   313   314   315   316   ...   555




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