— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet302/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   298   299   300   301   302   303   304   305   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

you are here
4
exception 
handling
321
The compiler needs to know 
that 
YOU know you’re calling 
a risky method.
If you wrap the risky code in something called a 
try/catch
, the compiler will relax. 
A try/catch block tells the compiler that you 
know an exceptional thing could happen in the 
method you’re calling, and that you’re prepared 
to handle it. That compiler doesn’t care how you 
handle it; it cares only that you say you’re taking 
care of it.
import javax.sound.midi.*;
public class MusicTest1 {
public void play() {

try {
Sequencer sequencer = MidiSystem. getSequencer();
System.out.println(“Successfully got a sequencer”);

} catch(MidiUnavailableException ex) {

System.out.println(“Bummer”);


}
 


// close play
public static void main(String[] args) {
MusicTest1 mt = new MusicTest1();
mt.play();

// close main

// close class
put the risky thing 
in a ‘try’ block.
make a ‘catch’ block for what to 
do if the exceptional situation 
happens -- in other words, a 
MidiUnavailableException 
is thrown 
by the call to getSequencer().
Dear Compiler,
I know I’m taking a risk here, but 
don’t you think it’s worth it? What 
should I do?
signed, geeky in Waikiki
Dear geeky,
Life is short (especially on the heap). 
Take the risk. 
try
it. But just in case 
things don’t work out, be sure to 
catch 
any problems before all hell breaks loose.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   298   299   300   301   302   303   304   305   ...   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