Is lesson me hum cover karenge:
- Exception kya hota hai
- Built-in exceptions
- throw keyword
- throws keyword
- Custom (User-defined) exception
- Exception flow aur real programs
Exception ek runtime problem hota hai jo program ke normal flow ko disturb karta hai.
Example:
10 / 0 → ArithmeticException
Agar exception handle na kiya jaye:
program terminate ho jata hai
Java already bahut saare exceptions provide karta hai.
| Exception | Cause |
|---|---|
| ArithmeticException | divide by zero |
| NullPointerException | null object |
| ArrayIndexOutOfBoundsException | invalid index |
| NumberFormatException | invalid number |
Example:
int a = 10 / 0; // ArithmeticExceptiontry {
risky code
} catch(Exception e) {
handling code
}Example:
try {
int a = 10 / 0;
} catch(ArithmeticException e) {
System.out.println("Cannot divide by zero");
}throw ka use hota hai:
manually exception generate karne ke liye
Syntax:
throw new ExceptionType("message");Example:
int age = 15;
if(age < 18){
throw new ArithmeticException("Not eligible");
}throws ka use hota hai:
method declaration me exception ko forward karne ke liye
Example:
void readFile() throws IOException {
}| Feature | throw | throws |
|---|---|---|
| Use | exception throw karna | exception declare karna |
| Location | method body | method signature |
| Object required | ✔ Yes | ❌ No |
Aap khud ki exception class bana sakte ho jab built-in exception sufficient na ho.
Example:
class InvalidAgeException extends Exception {
InvalidAgeException(String msg){
super(msg);
}
}static void checkAge(int age) throws InvalidAgeException {
if(age < 18){
throw new InvalidAgeException("Under age");
}
}public static void main(String[] args) {
try {
checkAge(15);
} catch(InvalidAgeException e){
System.out.println(e.getMessage());
}
}Output:
Under age
method → throw → throws → try-catch → handled
class InvalidAgeException extends Exception {
InvalidAgeException(String msg){
super(msg);
}
}
class Test {
static void checkAge(int age) throws InvalidAgeException {
if(age < 18){
throw new InvalidAgeException("Not eligible to vote");
}
}
public static void main(String[] args) {
try {
checkAge(16);
} catch(InvalidAgeException e){
System.out.println(e.getMessage());
}
}
}| Type | Example |
|---|---|
| Checked | IOException |
| Unchecked | ArithmeticException |
Custom exception default:
checked hoti hai
Agar RuntimeException extend kare:
unchecked ban jati hai
class MyException extends RuntimeException {
MyException(String msg){
super(msg);
}
}✔ throw ek exception object throw karta hai
✔ throws multiple exceptions declare kar sakta hai
✔ custom exception class Exception ya RuntimeException extend karti hai
- throw aur throws me difference kya hai?
- Custom exception kab banani chahiye?
- Checked aur unchecked custom exception me difference kya hai?
- Exception hierarchy kya hoti hai?
Is lesson me humne seekha:
✔ Exception handling basics
✔ Built-in exceptions
✔ throw keyword
✔ throws keyword
✔ Custom exception creation aur handling
Java me exception handling robust aur error-safe programs banane ke liye essential hai.