B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Java Exception Handling | Login Technologies

Java Exception Handling

Exception handling means writing code that can catch errors during program execution and respond properly without crashing the program. An exception is an error that occurs while the program is running. Imagine your Java program is reading a file, but the file doesn't exist — it will crash! Instead, you can handle this situation gracefully using exception handling and show a user-friendly message. Example exceptions include:

  • Dividing a number by 0
  • Trying to open a file that doesn't exist
  • Accessing an array index that doesn’t exist
The main keywords are try, catch, finally, throw, and throws.

Exception handling का मतलब है ऐसा code लिखना जो प्रोग्राम चलने के दौरान होने वाली errors को पकड़ सके और प्रोग्राम को crash होने से बचा सके। Exception एक error होता है जो प्रोग्राम के execution के दौरान होता है। सोचिए आपका Java प्रोग्राम कोई file पढ़ रहा है, लेकिन वह file मौजूद नहीं है — तो प्रोग्राम crash हो जाएगा! Exception handling से आप इस स्थिति को gracefully handle कर सकते हैं और user-friendly message दिखा सकते हैं। उदाहरण के तौर पर:

  • 0 से किसी संख्या को भाग देना
  • ऐसी file खोलने की कोशिश जो मौजूद नहीं है
  • ऐसे array index तक पहुंचना जो मौजूद नहीं है
मुख्य keywords हैं try, catch, finally, throw, और throws

Example 1: Basic try-catch
public class ExceptionDemo {
    public static void main(String[] args) {
        try {
            int result = 10 / 0; // ArithmeticException
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Cannot divide by zero!");
        }
        System.out.println("Program continues...");
    }
}

Output:

Cannot divide by zero!
Program continues...

Handles divide by zero exception gracefully using try-catch.

try-catch से divide by zero वाली error को संभाला गया है।

Example 2: Multiple catch blocks
public class MultipleCatch {
    public static void main(String[] args) {
        try {
            int[] arr = new int[5];
            arr[10] = 50; // ArrayIndexOutOfBoundsException
        } catch (ArithmeticException e) {
            System.out.println("Arithmetic Exception caught.");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Array index is out of bounds!");
        }
    }
}

Output:

Array index is out of bounds!

Shows handling of different exceptions with multiple catch blocks.

Multiple catch blocks से अलग-अलग exceptions को handle करना।

Example 3: finally block usage
public class FinallyDemo {
    public static void main(String[] args) {
        try {
            int data = 25 / 5;
            System.out.println("Result: " + data);
        } catch (NullPointerException e) {
            System.out.println("Null Pointer Exception");
        } finally {
            System.out.println("This block always executes.");
        }
    }
}

Output:

Result: 5
This block always executes.

finally block executes no matter what happens in try-catch.

finally block हमेशा execute होता है, चाहे exception आए या ना आए।

Example 4: throw keyword
public class ThrowDemo {
    static void checkAge(int age) {
        if (age < 18) {
            throw new ArithmeticException("Not eligible to vote");
        } else {
            System.out.println("Eligible to vote");
        }
    }
    public static void main(String[] args) {
        checkAge(15);
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: Not eligible to vote

Throws an exception manually using throw keyword.

throw keyword से manually exception फेंकना।

Example 5: throws keyword
import java.io.IOException;
public class ThrowsDemo {
    static void readFile() throws IOException {
        throw new IOException("File not found");
    }
    public static void main(String[] args) {
        try {
            readFile();
        } catch (IOException e) {
            System.out.println("Exception caught: " + e.getMessage());
        }
    }
}

Output:

Exception caught: File not found

Declares that a method throws an exception using throws keyword.

throws keyword से method में exception declare करना।

User-Defined Exception in Java (Custom Exception)

In Java, exceptions are objects that represent an error or unexpected behavior. Sometimes, Java's built-in exceptions like NullPointerException, IOException, etc., are not enough for your needs. So, you can create your own exception class — this is called a User-Defined Exception or Custom Exception.

Java में, exceptions ऐसे objects होते हैं जो errors या unexpected behavior को दर्शाते हैं। कभी-कभी Java के built-in exceptions जैसे NullPointerException, IOException आदि आपके काम के लिए पर्याप्त नहीं होते। इसलिए, आप अपनी खुद की exception class बना सकते हैं — इसे User-Defined Exception या Custom Exception कहते हैं।

Example 6: User-Defined Exception (Custom Exception)
class MyException extends Exception {
    public MyException(String message) {
        super(message);
    }
}

public class CustomExceptionDemo {
    static void checkValue(int value) throws MyException {
        if (value < 1) {
            throw new MyException("Value must be greater than zero");
        } else {
            System.out.println("Value is: " + value);
        }
    }

    public static void main(String[] args) {
        try {
            checkValue(0);
        } catch (MyException e) {
            System.out.println("Caught custom exception: " + e.getMessage());
        }
    }
}

Output:

Caught custom exception: Value must be greater than zero

Shows how to create and use a custom exception in Java.

Java में custom exception बनाने और उपयोग करने का उदाहरण।

← Previous:java interface Next: java multithreading introduction →