Java Checked Exceptions Must Be Handled

This confused me when I started working with exceptions public void test() throws Exception { throw new Exception("Error"); } Now see this: public static void main(String[] args) { test(); ❌ } 👉 This won’t compile! 💥 Error: Unhandled exception: Exception 👉 Why? Because test() is throwing a checked exception So Java forces you to handle it. ✅ Option 1: try { test(); } catch (Exception e) { e.printStackTrace(); } ✅ Option 2: public static void main(String[] args) throws Exception { test(); } 💡 Lesson: Checked exceptions must be handled either where they occur or where they are called. Did this confuse you earlier? 👇 #Java #CoreJava #ExceptionHandling #Programming #BackendDeveloper #Coding

To view or add a comment, sign in

Explore content categories