Metod overridinglə bağlı 3-cü qayda checked exceptionlar ilə əlaqəli idi. Həmin qaydalara yenidən baxaq və görək checked exceptionlar üçün hansı hallar mövcuddur:
1) Əgər superclassdakı metod heç bir exception fırlatmırsa, subclassdakı metod yeni checked exception fırlada bilməz:
class NewException extends Exception {}
class Hopper {
public void hop() {}
}
class Bunny extends Hopper {
public void hop() throws NewException {} // DOES NOT COMPILE
}
2) Yuxarıda qeyd etdiyimiz 1-ci halın tərsi mümkündür. Subclassdakı metod exception fırlatmasa belə, superclassdakı metod exception fırlada bilər:
class NewException extends Exception {}
class Hopper {
public void hop() throws NewException {}
}
class Bunny extends Hopper {
public void hop() {}
}
3) Subclassdakı metodun fırlatdığı exception superclassdakı metodun fırlatdığı exceptionun subclassı olmalıdır; daha kiçik və ya eyni tipə icazə verilir:
class NewException extends Exception {}
class Hopper {
public void hop() throws Exception {}
}
class Bunny extends Hopper {
public void hop() throws NewException {}
}
Təkrar olaraq vurğulayaq ki, bu qaydalar ancaq checked exceptionlara aiddir. Runtime exceptionlarla bağlı heç bir məhdudiyyət yoxdur:
class NewException extends Exception {}
class Hopper {
public void hop() {}
}
class Bunny extends Hopper {
public void hop()throws IllegalStateException {}
}
[topics lang=az]
