Mostly Asked Java Exception Handling Questions
Q1. What is exception handling in Java?What is exception handling in Java?
Exception handling is a mechanism to handle runtime errors, allowing the normal flow of the program to continue. It uses try, catch, finally blocks to catch exceptions and handle them gracefully.
Exception handling जावा में रनटाइम एरर को हैंडल करने की तकनीक है, जिससे प्रोग्राम का सामान्य प्रवाह बना रहता है। इसमें try, catch, finally ब्लॉक्स का उपयोग कर एक्सेप्शन को पकड़ा और संभाला जाता है।
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} finally {
System.out.println("Execution completed.");
}Q2. Difference between checked and unchecked exceptions?Difference between checked and unchecked exceptions?
Checked exceptions are checked at compile time and must be handled using try-catch or declared with throws. Examples: IOException, SQLException.
Unchecked exceptions occur at runtime and don't need mandatory handling. Examples: NullPointerException, ArithmeticException.
Checked exceptions compile टाइम पर जांचे जाते हैं और इन्हें try-catch से हैंडल करना या throws के साथ डिक्लेयर करना जरूरी है। उदाहरण: IOException, SQLException।
Unchecked exceptions रनटाइम पर होती हैं और इन्हें हैंडल करना अनिवार्य नहीं है। उदाहरण: NullPointerException, ArithmeticException।
Q3. What is the purpose of finally block?What is the purpose of finally block?
The finally block always executes after try and catch blocks, regardless of exception occurrence. It is used to release resources like closing files, database connections.
finally ब्लॉक try और catch के बाद हमेशा चलता है, चाहे एक्सेप्शन आए या न आए। इसका उपयोग संसाधनों को मुक्त करने जैसे फाइल या डेटाबेस कनेक्शन बंद करने के लिए होता है।
Q4. How to create a custom exception in Java?How to create a custom exception in Java?
Create a class extending Exception or RuntimeException and provide constructors. You can throw instances of this class to represent application-specific errors.
Exception या RuntimeException को एक्सटेंड करते हुए एक क्लास बनाएं और कंस्ट्रक्टर दें। आप इस क्लास के ऑब्जेक्ट को थ्रो कर सकते हैं जो एप्लीकेशन-विशिष्ट त्रुटियों का प्रतिनिधित्व करता है।
class EmployeeNotFoundException extends Exception {
public EmployeeNotFoundException(String message) {
super(message);
}
}Q5. What is the difference between throw and throws?What is the difference between throw and throws?
throw is used to explicitly throw an exception from a method or block.
throws is used in method signature to declare exceptions that might be thrown.
throw का उपयोग मेथड या ब्लॉक से एक्सेप्शन को थ्रो करने के लिए होता है।
throws का उपयोग मेथड सिग्नेचर में उन एक्सेप्शन्स को घोषित करने के लिए किया जाता है जो थ्रो हो सकते हैं।
void checkAge(int age) throws IllegalArgumentException {
if (age < 18) {
throw new IllegalArgumentException("Age must be 18 or older.");
}
}Q6. Can multiple catch blocks be used? Explain.Can multiple catch blocks be used? Explain.
Yes, multiple catch blocks can handle different types of exceptions separately. Java checks the catch blocks from top to bottom and executes the first matching one.
हाँ, अलग-अलग प्रकार के एक्सेप्शन्स को हैंडल करने के लिए कई catch ब्लॉक्स का उपयोग किया जा सकता है। Java टॉप से बॉटम तक चेक करता है और पहला मैचिंग catch ब्लॉक एक्सिक्यूट करता है।
try {
int[] arr = new int[5];
System.out.println(arr[10]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is out of bounds.");
} catch (Exception e) {
System.out.println("General exception caught.");
}Q7. What is the use of try-with-resources statement?What is the use of try-with-resources statement?
Try-with-resources automatically closes resources like streams or files after use, eliminating the need for explicit finally blocks to close them.
Try-with-resources का उपयोग संसाधनों जैसे स्ट्रीम्स या फाइल्स को ऑटोमेटिकली क्लोज़ करने के लिए किया जाता है, जिससे explicit finally ब्लॉक की जरूरत नहीं पड़ती।
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}Q8. What is the difference between Error and Exception?What is the difference between Error and Exception?
Errors are serious problems that applications should not catch (e.g., OutOfMemoryError). Exceptions are conditions that applications might want to catch and handle.
Errors गंभीर समस्याएं होती हैं जिन्हें एप्लीकेशन को कैच नहीं करना चाहिए (जैसे OutOfMemoryError)। Exceptions वे स्थिति हैं जिन्हें एप्लीकेशन कैच और हैंडल कर सकती है।
Q9. Explain the hierarchy of exception classes in Java.Explain the hierarchy of exception classes in Java.
Throwable is the superclass of all errors and exceptions.
- Error: Serious problems, not meant to be caught.
- Exception: Can be checked or unchecked.
-- Checked exceptions must be declared or caught.
-- Runtime exceptions (unchecked) need not be declared or caught.
Throwable सभी errors और exceptions की सुपरक्लास है।
- Error: गंभीर समस्याएं, जिन्हें कैच नहीं किया जाता।
- Exception: Checked और unchecked हो सकते हैं।
-- Checked exceptions को डिक्लेयर या कैच करना जरूरी है।
-- Runtime exceptions (unchecked) को डिक्लेयर या कैच करना जरूरी नहीं।
Q10. How do you handle multiple exceptions in one catch block?How do you handle multiple exceptions in one catch block?
Since Java 7, multiple exceptions can be caught in a single catch block using pipe '|' operator.
Java 7 से, pipe '|' ऑपरेटर का उपयोग करके एक ही catch ब्लॉक में कई एक्सेप्शन्स को हैंडल किया जा सकता है।
try {
// code
} catch (IOException | SQLException ex) {
ex.printStackTrace();
}