— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

322
 
chapter 11
I’m gonna 
TRY
this risky thing 
and I’m gonna 
CATCH
myself if I fall.
Don’t try this at home. 
try {

// do risky thing
} catch(Exception ex) {

// try to recover
}
An exception is an object...
of type Exception.
Which is fortunate, because it would be much harder 
to remember if exceptions were of type Broccoli. 
Remember from your polymorphism chapters that 
an object of type Exception can be an instance of any 
subclass of Exception.
Because an Exception is an object, what you catch is an 
object. In the following code, the 
catch 
argument 
is declared as type Exception, and the parameter 
reference variable is ex
it’s just like declaring 
a method argument.
This code only runs if an 
Exception is thrown.
InterruptedException
Throwable
Exception
IOException
getMessage()
printStackTrace()
Part of the Exception 
class hierarchy. They all 
extend class Throwable 
and inherit two key 
methods.
exceptions are 
objects
What you write in a catch block depends on the 
exception that was thrown. For example, if a server 
is down you might use the catch block to try another 
server. If the fi le isn’t there, you might ask the user 
for help fi nding it.


you are here
4
exception 
handling
323
If it’s 
your code that catches the exception
then whose code throws it?
You’ll spend much more of your Java coding time handling 
exceptions than you’ll spend creating and throwing them yourself. 
For now, just know that when your code calls a risky method—a 
method that declares an exception—it’s the risky method that 
throws the exception back to you, the caller. 
In reality, it might be you who wrote both classes. It really 
doesn’t matter who writes the code... what matters is knowing 
which method throws the exception and which method catches it.
When somebody writes code that could throw an exception, they 
must declare the exception.
One method will 
catch what another 
method throws. An 
exception is always 
thrown back to the 
caller.
The method that 
throws has to declare 
that it might throw 
the exception.

Download 40,57 Mb.

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