
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)
π 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());
}
}
β Output:
Exception in thread "main" java.lang.NullPointerException
π§ 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);
}
}
β Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
π§ 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]);
}
}
β Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
π§ 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];
}
}
β Output:
Exception in thread "main" java.lang.NegativeArraySizeException
π§ 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...");
}
}
β Output:
Handled Arithmetic Exception
Program continues...
π§ 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");
}
}
}
β Output:
Array Index Exception handled
π§ 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");
}
}
}
Output:
Null Pointer handled
πΉ 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");
}
}
}
β Output:
First exception handled
Second exception handled
π§ 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)