DEV Community

Cover image for πŸš€ Day 32 of My Automation Journey – Exception Handling
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 32 of My Automation Journey – Exception Handling


Today’s learning was all about Exception Handling πŸ”₯

πŸ‘‰ Handling runtime errors
πŸ‘‰ Writing safe programs
πŸ‘‰ Understanding different exception types

This is a must-know concept for writing real-world applications πŸš€


πŸ”Ή PART A – What is Exception Handling?

πŸ‘‰ An exception is an error that occurs during program execution

πŸ’‘ Instead of crashing the program, we can handle it gracefully


πŸ”Ή Why Exception Handling?

Without exception handling:
❌ Program stops immediately

With exception handling:
βœ” Program continues execution
βœ” Errors are handled properly

πŸ“˜ Exception Handling – Quick Revision Notes (Day 32)

While learning Exception Handling, I created these quick revision notes to clearly understand the concepts and hierarchy.


πŸ”Ή What is Exception Handling?

Exception handling is a mechanism in Java to handle runtime exceptions and maintain the normal flow of the application.

πŸ‘‰ Without handling β†’ program crashes
πŸ‘‰ With handling β†’ program continues smoothly


πŸ”Ή What is an Exception?

An exception is an unexpected event that:

βœ” Occurs during runtime
βœ” Disrupts normal program execution


πŸ”Ή Exception Hierarchy (Very Important πŸ”₯)

Throwable
   |
   |------ Error
   |
   |------ Exception
             |
             |------ Checked Exception
             |
             |------ Unchecked Exception (RuntimeException)
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Throwable is the parent class of all errors and exceptions


πŸ”Ή Errors

Errors represent serious problems in the application

πŸ”Έ Key Points:

❌ Cannot be handled using code
❌ Mostly system-level failures

πŸ”Έ Examples:

  • OutOfMemoryError
  • StackOverflowError

πŸ’‘ These are not meant to be handled in normal programs


πŸ”Ή Exceptions

Exceptions are manageable problems
πŸ‘‰ Can be handled using try-catch


πŸ”Ή Checked Exceptions (Compile-Time)

πŸ‘‰ Also known as compile-time exceptions

πŸ”Έ Key Points:

βœ” Detected by compiler (JDK)
βœ” Program will not execute until fixed
βœ” Must be handled or declared

πŸ”Έ Examples:

  • IOException
  • FileNotFoundException
  • SQLException
  • ClassNotFoundException

πŸ”Έ Common Causes:

  • Missing semicolon
  • Wrong datatype assignment
  • File handling issues

πŸ”Ή Unchecked Exceptions (Runtime)

πŸ‘‰ Also known as runtime exceptions

πŸ”Έ Key Points:

βœ” Occur during execution
βœ” Not checked by compiler
βœ” Handling is optional

πŸ”Έ Examples:

  • NullPointerException
  • ArithmeticException
  • ArrayIndexOutOfBoundsException

πŸ”₯ Checked vs Unchecked

Feature Checked Exception Unchecked Exception
Detected Compile time Runtime
Handling Mandatory Optional
Example IOException NullPointerException

πŸ”Ή Key Takeaways

βœ” Exception = runtime issue
βœ” Error = system-level failure
βœ” Checked β†’ compile-time
βœ” Unchecked β†’ runtime
βœ” Throwable = root class


πŸ’‘ Final Insight

πŸ‘‰ Understanding exceptions is not just about handling errors β€” it’s about writing stable and reliable programs πŸš€


πŸ”₯ My Learning

Today I understood:

  • Difference between error & exception
  • Compile-time vs runtime issues
  • Importance of exception hierarchy

πŸ”Ή PART B – Types of Exceptions


πŸ”₯ 1. Null Pointer Exception

βœ… Program:

public class NullPointerDemo {
    public static void main(String[] args) {
        String name = null;
        System.out.println(name.length());
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Output:

Exception in thread "main" java.lang.NullPointerException
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation:

πŸ‘‰ Trying to access method on null object


πŸ”₯ 2. Arithmetic Exception

βœ… Program:

public class ArithmeticDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 0;
        System.out.println(a / b);
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation:

πŸ‘‰ Division by zero is not allowed


πŸ”₯ 3. Array Index Out Of Bounds

βœ… Program:

public class ArrayIndexDemo {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30};
        System.out.println(arr[5]);
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation:

πŸ‘‰ Accessing index outside array size


πŸ”₯ 4. Negative Array Size Exception

βœ… Program:

public class NegativeArrayDemo {
    public static void main(String[] args) {
        int[] arr = new int[-5];
    }
}
Enter fullscreen mode Exit fullscreen mode

❌ Output:

Exception in thread "main" java.lang.NegativeArraySizeException
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation:

πŸ‘‰ Array size cannot be negative


πŸ”Ή PART C – Handling Exceptions Using try-catch


βœ… Program – Basic Handling

public class TryCatchDemo {
    public static void main(String[] args) {
        try {
            int a = 10;
            int b = 0;
            System.out.println(a / b);
        } catch (ArithmeticException e) {
            System.out.println("Handled Arithmetic Exception");
        }
        System.out.println("Program continues...");
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Handled Arithmetic Exception
Program continues...
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation:

πŸ‘‰ Exception is caught and handled
πŸ‘‰ Program continues execution


πŸ”Ή PART D – Multiple Catch Blocks


βœ… Program:

public class MultipleCatchDemo {
    public static void main(String[] args) {
        try {
            int[] arr = new int[3];
            System.out.println(arr[5]);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array Index Exception handled");
        } catch (Exception e) {
            System.out.println("General Exception handled");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Array Index Exception handled
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation:

πŸ‘‰ Specific exception handled first
πŸ‘‰ General exception acts as backup


πŸ”Ή PART E – One try, Multiple Catch

public class MultiErrorDemo {
    public static void main(String[] args) {
        try {
            String s = null;
            System.out.println(s.length());
        } catch (NullPointerException e) {
            System.out.println("Null Pointer handled");
        } catch (Exception e) {
            System.out.println("General Exception handled");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Null Pointer handled
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή PART F – try After catch (Important Concept πŸ”₯)

πŸ‘‰ Yes, we can write another try after catch


βœ… Program:

public class NestedTryDemo {
    public static void main(String[] args) {

        try {
            int a = 10 / 0;
        } catch (ArithmeticException e) {
            System.out.println("First exception handled");
        }

        try {
            String s = null;
            System.out.println(s.length());
        } catch (NullPointerException e) {
            System.out.println("Second exception handled");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

First exception handled
Second exception handled
Enter fullscreen mode Exit fullscreen mode

🧠 Explanation:

πŸ‘‰ After one try-catch, we can write another
πŸ‘‰ Each block handles its own exception


πŸ”Ή PART G – Important Rules ⚠️

βœ” Only one exception occurs at a time
βœ” Catch specific exception first
βœ” Exception class should be last
βœ” Program continues after handling


πŸ”š FINAL TAKEAWAYS

βœ” Exception = runtime error
βœ” try-catch prevents program crash
βœ” Multiple exceptions can be handled
βœ” Specific catch is important
βœ” Clean error handling = better code


πŸ’‘ GOLDEN INSIGHT

πŸ‘‰ Exception handling makes your program strong, safe, and production-ready πŸš€


Stay tuned πŸš€

Top comments (0)