Try और Catch

Java में डेटा टाइप्स और Exceptions

Jim White

Java Developer

Java में समस्याएँ

  • एप्लिकेशन में समस्याएँ/इश्यू आ सकते हैं
    • गलत इनपुट के कारण
    • कोडिंग एरर के कारण
    • अप्रत्याशित हालात के कारण
  • ऐसी समस्याओं को exceptions कहा जाता है
    • सामान्य control flow बाधित होता है
  • Java exceptions throws करता है
Java में डेटा टाइप्स और Exceptions

Exception उदाहरण

public static void main(String[] args) {
    ArrayList<String> allStars = new ArrayList<String>();
    allStars.add("Mays");
    allStars.add("Aaron");
    allStars.add("Ruth");
    String last = allStars.get(3); // This will cause an exception
    System.out.println(last);
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of 
bounds for length 3
Java में डेटा टाइप्स और Exceptions

Try

  • exception पैदा करने वाले कोड को try-block में रखें
    • "यह कोड चलाएँ, और समस्या हो तो catch ब्लॉक को पास करें"
try {
    // Statements जिन्हें चलाना है और exceptions के लिए देखना है
}
Java में डेटा टाइप्स और Exceptions

Catch

  • try के बाद catch-block रखें
    • exception समस्याओं को संभालने वाला handler होता है
    • जो exception पकड़ा गया है, वह पैरामीटर के रूप में इस ब्लॉक में आता है
  • कुछ प्रकार के exceptions के लिए try/catch जरूरी है
    • दूसरों के लिए वैकल्पिक
try {
    // Statements जिन्हें चलाना है और exceptions के लिए देखना है
} catch (<Exception type> <variable name>) {
    // Exception होने पर चलने वाले statements
}
Java में डेटा टाइप्स और Exceptions

IndexOutOfBoundsException पकड़ना

try {
  ArrayList<String> allStars = new ArrayList<String>();
  allStars.add("Mays");
  allStars.add("Aaron");
  allStars.add("Ruth");
  String last = allStars.get(3); // Accessing an element that isn't there
  System.out.println(last);
} catch (IndexOutOfBoundsException e) {
  System.out.println("Oops - wrong index");
}
Oops - wrong index
Java में डेटा टाइप्स और Exceptions

Multiple Catch

  • अलग-अलग प्रकार के exceptions संभालने के लिए कई catch ब्लॉक इस्तेमाल करें
  • हर catch का अपना exception type और exception वैरिएबल होता है
  • Exception होने पर Java क्रम से catch ब्लॉक्स चेक करता है, जो मैच मिलता है वही चलता है
try {
    ArrayList<String> nums 
      = new ArrayList<String>();
    nums.add("one");
    String last = nums.get(0);
    Integer.valueOf(last);
    System.out.println(last);
} catch (IndexOutOfBoundsException eIndex) {
    System.out.println("Oops - wrong index");
} catch (NumberFormatException eValueOf) {
    System.out.println(
      "Oops - String not a number");
}
Oops - String not a number
Java में डेटा टाइप्स और Exceptions

Catch "All"

  • catch (Exception variable){ } को catch-all की तरह इस्तेमाल करें
    • वे सभी exceptions संभालता है जिन्हें ऊपर के catch ब्लॉक्स नहीं संभालते
try {
    // Code to try
} catch (IndexOutOfBoundsException eIndex) {
    System.out.println("Oops - wrong index");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops - String not a number");
} catch (Exception e) { // Handles all exceptions except the two above
    System.out.println("Something unplanned happened");
}
Java में डेटा टाइप्स और Exceptions

Finally

  • finally ब्लॉक का कोड हमेशा execute होता है
// जब exception होता है
try {
    Integer.valueOf("one");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops");
} finally {
    System.out.println("Doing cleanup");
}
Oops
Doing cleanup

 

// जब कोई exception throw नहीं होता
try {
    Integer.valueOf("1");
} catch (NumberFormatException eValueOf) {
    System.out.println("Oops");
} finally {
    System.out.println("Doing cleanup");
}
Doing cleanup
Java में डेटा टाइप्स और Exceptions

Exception ऑब्जेक्ट

  • catch ब्लॉक में आने वाला Exception ऑब्जेक्ट बहुत जानकारी देता है
  • क्या गलती हुई, यह जानने के लिए उसकी methods का उपयोग करें
    • मानवीय-पठनीय कारण के लिए .getMessage()
    • Exception की class/type जानने के लिए .getClass()
    • stack trace दिखाने के लिए .printStackTrace() (अगली स्लाइड)
public class ExceptionExample {

    public static void main(String[] args) {
        try {
            Integer.valueOf("one");
        } catch (NumberFormatException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
Java में डेटा टाइप्स और Exceptions

Exception stack trace

  • stack trace समस्या तक पहुँचने वाले execution flow का रिकॉर्ड देता है
    • नीचे: पिछले कोड उदाहरण का आउटपुट - जिसमें प्रिंट हुआ stack trace शामिल है
For input string: "one"
java.lang.NumberFormatException: For input string: "one"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Integer.parseInt(Integer.java:661)
    at java.base/java.lang.Integer.valueOf(Integer.java:988)
    at foo/exceptions.ExceptionExample.main(ExceptionExample.java:7)
Java में डेटा टाइप्स और Exceptions

अभ्यास करते हैं!

Java में डेटा टाइप्स और Exceptions

Preparing Video For Download...