Kotlin for Android Developers



Download 1,04 Mb.
Pdf ko'rish
bet123/125
Sana11.08.2021
Hajmi1,04 Mb.
#144654
1   ...   117   118   119   120   121   122   123   124   125
Bog'liq
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )


2
#ff212121
3
@android:color/black
4
28.4 Exceptions
In Kotlin, all exceptions implement
Throwable
, have a message and are unchecked. This means we
are not obliged to use
try/catch
on any of them. That’s not the case in Java, where methods that
throw
IOException
, for instance, need to be surrounded by a
try/catch
block. Years of dealing
with them have shown that checked exceptions were not a good idea. People like
Bruce Eckel³¹
,
Rod
Waldhoff³²
or
Anders Hejlsberg³³
can give you a better perspective about it.
The way to throw an exception is very similar to Java:
1
throw MyException("Exception message")
And
try
expression is identical too:
1
try {
2
// some code
3
}
4
catch (e: SomeException) {
5
// handler
6
}
7
finally {
8
// optional finally block
9
}
Both
throw
and
try
are expressions in Kotlin, which means they can be assigned to a variable. This
is really useful when dealing with edge cases:
³¹
http://www.mindview.net/Etc/Discussions/CheckedExceptions
³²
http://radio-weblogs.com/0122027/stories/2003/04/01/JavasCheckedExceptionsWereAMistake.html
³³
http://www.artima.com/intv/handcuffs.html


28 Extra concepts
143
1
val s = when(x){
2
is Int -> "Int instance"
3
is String -> "String instance"
4
else -> throw UnsupportedOperationException("Not valid type")
5
}
or
1
val s = try { x as String } catch(e: ClassCastException) { null }



Download 1,04 Mb.

Do'stlaringiz bilan baham:
1   ...   117   118   119   120   121   122   123   124   125




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