— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

338
 
chapter 11
fixing the 
Sequencer
 code 
public void play() {
try {
Sequencer sequencer = MidiSystem.getSequencer();
System.out.println(“Successfully got a sequencer”);
} catch(MidiUnavailableException ex) {
System.out.println(“Bummer”);
}
} // close play
Now that you’ve completely forgotten, we started this chapter 
with a first look at some JavaSound code. We created a Se-
quencer object but it wouldn’t compile because the method 
Midi.getSequencer() declares a checked exception (MidiUnavail-
ableException). But we can fix that now by wrapping the call in a 
try/catch.
Getting back to our music code...
No problem calling getSequencer(), 
now that we’ve wrapped it in a try/
catch block.
The catch parameter has to be 
the ‘right’ exception. If we said 
‘catch(FileNot FoundException f), the 
code would not compile, because poly-
morphically a MidiUnavilableException 
won’t fit into a FileNotFoundException. 
Remember it’s not enough to have a 
catch block... you have to catch the 
thing being thrown!
You cannot have a catch or finally 
without a try
Exception Rules
void go() {
Foo f = new Foo();
f.foof();
catch(FooException ex) { }
}
NOT LEGAL!
Where’s the try?
You cannot put code between the 
try and the catch
try {
x.doStuff();
}
int y = 43;
} catch(Exception ex) { }
NOT LEGAL
! You can’t put 
code between the try and 
the catch.
A try MUST be followed by either a 
catch or a finally
try {
x.doStuff();
} finally {
// cleanup
}
LEGAL because you 
have a finally, even 
though there’s 
no catch. 
But you cannot have a 
try by itself.

Download 40,57 Mb.

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