Ruslan Mukhamadiarov’s Post

🚨 𝗧𝘄𝗼 𝗳𝗮𝗶𝗹𝘂𝗿𝗲𝘀 𝗵𝗮𝗽𝗽𝗲𝗻𝗲𝗱. 𝗬𝗼𝘂𝗿 𝗹𝗼𝗴 𝘀𝗵𝗼𝘄𝘀 𝗼𝗻𝗲. Most developers know that try-with-resources closes resources automatically. But many miss this: ⚠️ if the try block fails, and then close() fails too, the close() exception becomes suppressed. And sometimes that second exception is exactly what tells you whether the output can still be trusted. Example 👇 try ( OutputStream out = Files.newOutputStream(path); ZipOutputStream zip = new ZipOutputStream(out) ) { writeEntries(zip); } Now imagine this actually happened: 📌 Primary exception writeEntries(zip) failed 📌 Suppressed exception zip.close() failed At first glance, you may think: ▪ one record failed ▪ export failed ▪ investigate the business/data issue But that is only half the story. Because for resources like ZipOutputStream, close() is not just cleanup. It may still need to: ▪ finish the ZIP structure ▪ flush remaining bytes ▪ write the final directory/footer So the suppressed exception can tell you something critical: 🚫 the output file may be incomplete or corrupted That changes the conclusion. Without the suppressed exception, you may think: ✅ "The export failed because of one bad record." With the suppressed exception, the better conclusion is: ❌ "The ZIP itself may be invalid. Do not trust the file." And here is how teams often hide that second failure: log.error("Export failed: {}", e.getMessage()); Now the log shows only the primary exception. The suppressed one is still there... but easy to miss. You need this: log.error("Export failed", e); And when needed, inspect: e.getSuppressed() 🧠 𝗧𝗵𝗲 𝗿𝗲𝗮𝗹 𝗹𝗲𝘀𝘀𝗼𝗻: The primary exception tells you why the operation failed. The suppressed exception may tell you whether the output can still be trusted. That is a huge difference in production. Especially for: 📦 ZIP / GZIP export 📄 CSV generation 💾 file writing 🌐 response/output streams ☁️ upload streams Miss the suppressed exception - miss half the story. Have you ever found the real operational clue inside getSuppressed()? 🤔 #Java #Backend #Exceptions #JavaIO #SoftwareEngineering #Debugging #CleanCode

  • graphical user interface, diagram, text

To view or add a comment, sign in

Explore content categories