— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

you are here
4
exception 
handling
337
So now we’ve seen both ways to satisfy the compiler 
when you call a risky (exception-throwing) method.
Handle or Declare. It’s the law.
HANDLE
1
try {
laundry.doLaundry();
} catch(ClothingException
cex) {
// recovery code
}
Wrap the risky call in a try/catch
This had better be a big enough catch to 
handle all exceptions that doLaundry(

might throw. Or else the compiler will 
still complain that you’re not catching all 
of the exceptions.
DECLARE (duck it)
2
void foo()
 throws ClothingException {
 
laundry.doLaundry();
}
Declare that YOUR method throws the same exceptions 
as the risky method you’re calling.
But now this means that whoever calls the foo() method 
has to follow the Handle or Declare law. If foo() ducks 
the exception (by declaring it), and main() calls foo(), then 
main() has to deal with the exception. 
public class Washer {
Laundry laundry = new Laundry();
public void foo() throws ClothingException {
laundry.doLaundry();
}
public static void main (String[] args) {
Washer a = new Washer();
a.foo();
}
}
Because the foo() method ducks the 
ClothingException thrown by doLaundry(), 
main() has to wrap a.foo() in a try/catch, 
or main() has to declare that it, too, 
throws ClothingException!
Now main() won’t compile, and we 
get an “unreported 
exception” error. 
As far as the compiler’s concerned, 
the foo() method throws an 
exception.
TROUBLE!!
The doLaundry() method throws a 
ClothingException, but by declaring the 
exception, the foo() method gets to 
duck the exception. No try/catch.



Download 40,57 Mb.

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