— Scott McNealy, Sun Microsystems Chairman, President and ceo


public void foo() throws ReallyBadException {



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

public void foo() throws ReallyBadException {
// call risky method without a try/catch
laundry.doLaundry();
}
You don’t REALLY throw it, but 
since you don’t have a try/catch 
for the risky method you call, 
YOU are now the “risky method”. 
Because now, whoever calls YOU 
has to deal with the exception.


336
 
chapter 11
handle or 
declare
Ducking (by declaring) only 
delays the inevitable
Sooner or later, 
somebody has to 
deal with it. But what if 
main() 
ducks the exception?
public class Washer {
Laundry laundry = new Laundry();
public void foo() 
throws ClothingException
{
laundry.doLaundry();
}
public static void main (String[] args) 
throws ClothingException
{
Washer a = new Washer();
a.foo();
}
}
Both methods duck the exception 
(by declaring it
) so there’s nobody to 
handle it
! This compiles just fine.
main
main
foo
doLaundry
main
foo
1
main() calls foo()
foo() calls doLaundry()
doLaundry() is 
running and throws a 
ClothingException
2
doLaundry() pops off the 
stack immediately and 
the exception is thrown 
back to foo().
But foo() doesn’t have a 
try/catch, so...
doLaundry() throws a 
ClothingException
foo() ducks the 
exception
3
main() ducks the 
exception
foo() pops off the 
stack immediately and 
the exception is thrown 
back to... who? What? 
There’s nobody left 
but the JVM, and it’s 
thinking, “Don’t expect 
ME to get you out of 
this.”
4
The JVM 
shuts down
We’re using the tee-shirt to represent a Clothing 
Exception. We know, we know... you would 
have preferred the blue jeans.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   311   312   313   314   315   316   317   318   ...   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