— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

flow control 
exercise


you are here
4
exception 
handling
329
public class Foo {
public void go() {
Laundry laundry = new Laundry();
try {
laundry.doLaundry();
} catch(PantsException pex) {
// recovery code
} catch(LingerieException lex) {
// recovery code
}
}
}
A method can throw multiple exceptions if it darn well needs to. But 
a method’s declaration must declare all the checked exceptions it can 
throw (although if two or more exceptions have a common superclass, the 
method can declare just the superclass.)
Did we mention that a method can 
throw more than one exception?
The compiler will make sure that you’ve handled all the checked excep-
tions thrown by the method you’re calling. Stack the catch blocks under 
the try, one after the other. Sometimes the order in which you stack the 
catch blocks matters, but we’ll get to that a little later.
Catching multiple exceptions
public class Laundry {
public void doLaundry() 
throws PantsException, LingerieException
{
// code that could throw either exception
}
}
This method declares two, count ‘em, 
TWO exceptions. 
if doLaundry() throws a 
PantsException, it lands in the 
PantsException catch block.
if doLaundry() throws a 
LingerieException, it lands in the 
LingerieException catch block.


330
 
chapter 11
Exceptions are objects, remember. There’s nothing all that 
special about one, except that it is a thing that can be thrown
So like all good objects, Exceptions can be referred to 
polymorphically. A LingerieException object, for example, 
could be assigned to a ClothingException reference. A 
PantsException could be assigned to an Exception reference. 
You get the idea. The benefi t for exceptions is that a method 
doesn’t have to explicitly declare every possible exception it 
might throw; it can declare a superclass of the exceptions. 
Same thing with catch blocks—you don’t have to write a catch 
for each possible exception as long as the catch (or catches) 
you have can handle any exception thrown.
Exceptions are polymorphic
You can DECLARE exceptions using 
a supertype of the exceptions you 
throw.
All exceptions have 
Exception as a 
superclass.

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   306   307   308   309   310   311   312   313   ...   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