
#java #exceptions #programming #backend
Java Concepts I’m Mastering – Part 11: Exception Handling (try-catch-finally) Continuing my journey of mastering Java fundamentals. Today’s concept: Exception Handling — because real programs must handle errors gracefully. What is an Exception? An exception is: An unexpected event that disrupts normal program flow. Examples: Dividing by zero Accessing invalid array index Reading a missing file If not handled → program crashes. try-catch Block We use try to write risky code and catch to handle errors. try { int result = 10 / 0; } catch (ArithmeticException e) { System.out.println("Cannot divide by zero!"); } Instead of crashing, it prints a message. finally Block The finally block: Always executes Used for cleanup (closing files, DB connections, etc.) try { System.out.println("Trying..."); } catch (Exception e) { System.out.println("Error occurred"); } finally { System.out.println("This always runs"); } Types of Exceptions Checked Exceptions (compile-time) Must be handled Example: IOExc
Continue reading on Dev.to
Opens in a new tab



