Java hataları try/catch/finally ile işler ve benzersiz şekilde checked exceptions (bildiri veya işleme tabi) ile unchecked exceptions (çalışma zamanında, zorunlu değil) arasında ayrım yapar. Bu ayrımı anlamak Java hata işleme için merkezidir.
Java hataları try/catch/finally ile işler ve benzersiz şekilde checked exceptions (bildiri veya işleme tabi) ile unchecked exceptions (çalışma zamanında, zorunlu değil) arasında ayrım yapar. Bu ayrımı anlamak Java hata işleme için merkezidir.
try {
FileReader f = new FileReader("file.txt"); // may throw IOException
// ... use it ...
} catch (FileNotFoundException e) {
System.out.println("not found: " + e.getMessage()); // specific first
} catch (IOException e) {
System.out.println("io error"); // broader after
} finally {
System.out.println("always runs — cleanup"); // runs no matter what
}
finally her zaman çalışır (temizlik), exception atılsa veya return gerçekleşse bile.
Throwable
├── Error — serious JVM problems (OutOfMemoryError) — don't catch
└── Exception
├── RuntimeException → UNCHECKED (not enforced by the compiler)
│ NullPointerException, IllegalArgumentException, IndexOutOfBounds...
└── (other Exceptions) → CHECKED (compiler enforces handling)
IOException, SQLException...
// MUST either catch it or declare `throws` — the compiler won't let you ignore it
public void readFile() throws IOException { // declare it
Files.readAllLines(Paths.get("file.txt")); // throws checked IOException
}
Checked exceptions (IOException, SQLException) kurtarılabilir, beklenen koşulları temsil eder (dosya eksik, ağ hatası). Derleyici sizi bunları işlemeye veya bildirmeye zorlar — dış hata işlemeyi açık hale getirir.
// NOT required to declare or catch — usually indicate PROGRAMMING BUGS
String s = null;
s.length(); // NullPointerException (unchecked)
int x = arr[10]; // ArrayIndexOutOfBoundsException (unchecked)
Integer.parseInt("abc"); // NumberFormatException (unchecked)
Unchecked (RuntimeException alt sınıfları) genellikle programlama hatalarını gösterir — hatayı her yerde yakalamak yerine düzeltirsiniz. Derleyici işlemeyi zorunlu kılmaz.
// resources implementing AutoCloseable are closed automatically
try (FileReader f = new FileReader("file.txt")) {
// use f
} // f.close() called automatically, even on exception — no finally needed
✓ Catch specific exceptions, not bare Exception
✓ Use try-with-resources for files/connections (auto-close)
✓ Don't swallow exceptions silently (empty catch blocks)
✓ Throw meaningful exceptions; include context in messages
✓ Custom exceptions extend Exception (checked) or RuntimeException (unchecked)
Sağlam hata işleme gereklidir ve Java'nın checked/unchecked ayrımı tanımlayan (ve tartışılan) bir özellik derinlemesine anlaşılmaya değer.
Checked exceptions beklenen harici hataların (I/O, DB) açık işlenmesini zorlar — güvenilirliği teşvik eder ancak bazen ayrıntılı olarak eleştirilir; unchecked exceptions her yerde rutin olarak yakalamak yerine düzeltilmesi gereken hataları temsil eder.
Hiyerarşiyi, her türün ne zaman uygulanacağını, try/catch/finally semantiğini ve otomatik temizlik için modern try-with-resources bilmek, doğru ve güvenilir Java yazmanın temelini oluşturur — ve kendi exceptions'larınızı uygun şekilde tasarlamanızı sağlar.
Ayrım, Java API'lerinin nasıl tasarlandığını belirler ve sık görülen bir mülakat ve kod inceleme konusudur.