Checked exception
RuntimeException'ın tersine, tüm checked exception'lar için bir try-catch bloğu olmalı; aksi halde kod derlenmez, çalışması bir yana. Bu egzersizde, bir checked exception etrafına try/catch eklemen gerektiğini, yoksa uygulamanın derlenmeyeceğini ve çalışmayacağını göreceksin.
Bu egzersiz, kursun bir parçasıdır
Java'da Veri Türleri ve İstisnalar
Egzersiz talimatları
Class.forName()çağrısının ele alınmasını gerektirdiğini ve ele alınmazsa derleme hatasına (error: compilation failed) yol açtığını görmek için örnek kodu hiç değiştirmeden çalıştır.- Try, catch ve catch bloğunda gösterilen mesajın yorumlarını kaldır.
- Şimdi uygulamayı tekrar çalıştır ve checked exception ele alındığı için uygulamanın derlenip çalıştığını gör.
Uygulamalı etkileşimli egzersiz
Bu egzersizi bu örnek kodu tamamlayarak deneyin.
public class CheckedExceptionHandling {
public static void main(String[] args) {
// Uncomment the try block
// try {
Class.forName("java.util.ArrayList");
// Uncomment the catch block for ClassNotFoundException
// } catch (ClassNotFoundException e) {
// Uncomment the message for the exception
// System.out.println("Work goes here to recover from the checked exception");
// Uncomment the end of the try/catch
// }
System.out.println("Work complete");
}
}