
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
🔹 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 🚀
SOCIAL SHARE CARD GENERATOR