Exception Handling in Depth

📘 Java 👁 69 views 📅 Dec 01, 2025
⏱ Estimated reading time: 2 min

Exception handling in Java is a mechanism to handle runtime errors so that the normal flow of the program is maintained. An exception is an event that disrupts the normal execution of a program.


1. Types of Exceptions in Java

Java divides exceptions into two main categories:

(a) Checked Exceptions

  • Checked by the compiler at compile time.

  • Must be handled using try-catch or declared with throws.

  • Examples: IOException, SQLException, ClassNotFoundException.

(b) Unchecked Exceptions

  • Checked at runtime, not by the compiler.

  • Usually caused by programming errors.

  • Examples: ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException.


2. Key Keywords in Exception Handling

KeywordPurpose
tryBlock of code to monitor for exceptions
catchBlock to handle the exception
finallyBlock executed regardless of exception occurrence
throwUsed to explicitly throw an exception
throwsDeclares the exceptions a method can throw

3. Syntax of Exception Handling

try { // Code that may throw exception } catch (ExceptionType e) { // Code to handle exception } finally { // Optional block, always executes }

Example:

public class Test { public static void main(String[] args) { try { int a = 10, b = 0; int result = a / b; // May throw ArithmeticException System.out.println(result); } catch (ArithmeticException e) { System.out.println("Error: Cannot divide by zero"); } finally { System.out.println("Execution completed"); } } }

Output:

Error: Cannot divide by zero Execution completed

4. Propagating Exceptions with throws

A method can declare the exceptions it may throw using throws.
Example:

void readFile() throws IOException { FileReader file = new FileReader("input.txt"); }

5. Creating Custom Exceptions

Java allows defining user-defined exceptions by extending the Exception class.

class MyException extends Exception { MyException(String msg) { super(msg); } } public class Test { void check(int age) throws MyException { if(age < 18) throw new MyException("Age must be 18 or above"); } }

6. Advantages of Exception Handling

  • Maintains normal flow of program.

  • Centralized error handling.

  • Separates error-handling code from normal code.

  • Provides descriptive messages for debugging.

  • Improves program robustness and reliability.


7. Summary Table

ConceptDescription
tryMonitors code for exceptions
catchHandles the exception
finallyExecutes always, optional cleanup
throwExplicitly throws an exception
throwsDeclares exceptions in method signature
CheckedCompile-time exception
UncheckedRuntime exception

8. Conclusion

Exception handling in Java ensures programs can gracefully handle runtime errors, improving reliability, maintainability, and user experience. Proper use of try-catch, finally, throw, and throws is essential for robust Java applications.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes