— Scott McNealy, Sun Microsystems Chairman, President and ceo


block runs. When the finally block completes



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

block runs. When the finally block completes, 
the rest of the method continues on. 
If the try block succeeds (
no exception), 
flow control skips over the catch block and 
moves to the finally block. When the finally 
block completes, the rest of the method 
continues on.
If the try or catch block has a return 
statement, finally will still run! 
Flow 
jumps to the finally, then back to the return.
File Edit Window Help RiskAll
%java Tester
We made it!


328
 
chapter 11
Look at the code to the left. What do you think the 
output of this program would be? What do you think 
it would be if the third line of the program were 
changed to:
String test = “yes”; 
?

Assume ScaryException extends Exception.
Sharpen your pencil
public class TestExceptions {
public static void main(String [] args) {
String test = “no”;
try {
System.out.println(“start try”);
doRisky(test);
System.out.println(“end try”);
} catch ( ScaryException se) {
System.out.println(“scary exception”);
} finally {
System.out.println(“finally”);
}
System.out.println(“end of main”); 
}
static void doRisky(String test) throws ScaryException {
System.out.println(“start risky”);
if (“yes”.equals(test)) {
throw new ScaryException();
}
System.out.println(“end risky”);
return;
}
}
 Output when 
test = “no”
 Output when 
test = “yes”
When t
est = “no
”: star
t try - star
t risk
y - end risk
y - end try - finally - end of main
When t
est = “y
es”: star
t try - star
t risk
y - scar
y exc
eption - finally - end of main
Flow Control

Download 40,57 Mb.

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