What are Exceptions?
Exception ka matlab hai program ke run hone ke dauraan aayi koi error ya unusual situation. Jaise:
- File ka nhi milne
- Divide by zero karna
- Null value pe operation karna
Java mein exceptions runtime pe aate hain aur agar handle nahi kiya to program crash ho sakta hai.
int a = 5 / 0; // ArithmeticException
Checked vs Unchecked Exceptions
Java exceptions two types ke hote hain:
1. Checked Exceptions:
- Ye compile time pe check ki jaati hain.
- Jaise:
IOException
,SQLException
- Tumhe ya toh inhe handle karna padta hai
try-catch
se, ya throw karna padna haithrows
keyword lagana padta hai.
public void readFile() throws IOException {
FileReader fr = new FileReader("data.txt");
}
2. Unchecked Exceptions:
- Ye runtime pe aate hain.
- Jaise:
NullPointerException
,ArithmeticException
- Inhe compiler check nahi karta.
String s = null;
System.out.println(s.length()); // NullPointerException
try-catch-finally Block
Java mein exception handle karne ke liye try-catch-finally
ka use hota hai.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Can't divide by zero!");
} finally {
System.out.println("This block always executes.");
}
try
mein likhte hain risky code.catch
exception handle karta hai.finally
block hamesha run hota hai (chahe exception aaye ya na aaye).
throw and throws Keywords
throw aur throws similar lagte hain, par dono ka kaam alag hai:
throw:
- Ye kisi specific exception ko manually throw karta hai.
throw new ArithmeticException("Custom error message");
throws:
- Ye method ke signature mein use hota hai jab method koi checked exception throw karta hai.
public void readFile() throws IOException {
// file handling code
}
Custom Exceptions
Kabhi-kabhi tumhe apni specific situation ke liye custom exception banana padta hai.
class AgeException extends Exception {
AgeException(String msg) {
super(msg);
}
}
public class Test {
public void checkAge(int age) throws AgeException {
if (age < 18) {
throw new AgeException("You must be 18+ to vote");
}
}
}
Use karte time:
Test t = new Test();
try {
t.checkAge(16);
} catch (AgeException e) {
System.out.println(e.getMessage());
}
Entire Code:
// Step 1: Custom Exception class banate hain
class AgeException extends Exception {
public AgeException(String message) {
super(message); // super = Exception class ka constructor
}
}
// Step 2: Main class jahan exception handle hoga
public class CustomExceptionDemo {
// Method to check age
public static void checkAge(int age) throws AgeException {
if (age < 18) {
throw new AgeException("You must be at least 18 years old.");
} else {
System.out.println("Age valid! You are eligible.");
}
}
// Step 3: main() method to test
public static void main(String[] args) {
int userAge = 16; // Tum isko change karke test kar sakte ho
try {
checkAge(userAge);
} catch (AgeException e) {
System.out.println("Custom Exception Caught:");
System.out.println(e.getMessage());
}
System.out.println("Program continues...");
}
}
Custom Exception Caught:
You must be at least 18 years old.
Program continues...
Try-with-Resources (Java 7+)
Agar tum file, database ya kisi bhi resource ke saath kaam kar rahe ho jo close karna padta hai, to try-with-resources
ka use karo.
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
Yahan resource (file reader) automatically close ho jati hai — koi finally
block ki zarurat nahi hoti.
Developer Tips:
- Exception handling code hamesha clean aur minimal rakho.
- Har exception ko catch karne ki bajaye specific exception pakdo.
- Don’t ignore exceptions silently – 'log' ya handle zaroor karo.
Summary
- Exception = error jo runtime pe aata hai
- Checked = compile time pe handle karna padta hai
- Unchecked = runtime errors hote hain
- try-catch-finally se exception handle hota hai
- throw/throws keywords se exceptions manage hote hain
- Custom exceptions se tum apne use-case ke errors define kar sakte ho
- Try-with-resources se tumhara resource auto-close ho jata hai
Comments