Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet269/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   265   266   267   268   269   270   271   272   ...   341
Bog'liq
Effective Java



CHAPTER 10
EXCEPTIONS
302
Item 73: Throw exceptions appropriate to the abstraction
It is disconcerting when a method throws an exception that has no apparent
connection to the task that it performs. This often happens when a method
propagates an exception thrown by a lower-level abstraction. Not only is it
disconcerting, but it pollutes the API of the higher layer with implementation
details. If the implementation of the higher layer changes in a later release, the
exceptions it throws will change too, potentially breaking existing client programs.
To avoid this problem, 
higher layers should catch lower-level exceptions
and, in their place, throw exceptions that can be explained in terms of the
higher-level abstraction.
This idiom is known as 
exception translation
:
// Exception Translation
try {
... // Use lower-level abstraction to do our bidding
} catch (LowerLevelException e) {
throw new HigherLevelException(...);
}
Here is an example of exception translation taken from the
AbstractSequentialList
class, which is a 
skeletal implementation
(Item 20) of
the 
List
interface. In this example, exception translation is mandated by the
specification of the 
get
method in the 
List
interface:
/**
* Returns the element at the specified position in this list.
* @throws IndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= size()}).
*/
public E get(int index) {
ListIterator i = listIterator(index);
try {
return i.next();
} catch (NoSuchElementException e) {
throw new IndexOutOfBoundsException("Index: " + index);
}
}
A special form of exception translation called 
exception chaining
is called for
in cases where the lower-level exception might be helpful to someone debugging
the problem that caused the higher-level exception. The lower-level exception (the


ITEM 73: THROW EXCEPTIONS APPROPRIATE TO THE ABSTRACTION
303
cause
) is passed to the higher-level exception, which provides an accessor method
(
Throwable
’s 
getCause
method) to retrieve the lower-level exception:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   265   266   267   268   269   270   271   272   ...   341




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