Errors and Exceptions

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

Jim White

Java Developer

Exceptions बनाम Errors

  • Error एक गंभीर समस्या है
  • एप्लिकेशन errors को संभाल नहीं सकते और न ही उनसे रिकवर कर सकते हैं
    • errors एप्लिकेशन को "crash" या बंद कर देते हैं
    • उदाहरण: Java में out of memory समस्या एक error है
  • एप्लिकेशन exceptions को संभाल सकते हैं और उनसे रिकवर कर सकते हैं
    • array की size से बाहर elements एक्सेस करना एक exception है.
Java में डेटा टाइप्स और Exceptions
Error उदाहरण
import java.util.*;

public class CauseOutOfMemory {
    public static void main(String[] args) {
        List<Long> numbers = new ArrayList<Long>();
        long counter = 0;
        while (true) {
            numbers.add(counter);
            counter++;
        }
    }
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.base/java.lang.Long.valueOf(Long.java:1204)
    at foo/exceptions.CauseOutOfMemory.main(CauseOutOfMemory.java:12)
Java में डेटा टाइप्स और Exceptions

Exceptions के दो प्रकार

Checked Exception Runtime ("unchecked") Exception
Require handling Yes No
General cause हमारे नियंत्रण से बाहर की चीज़ें हमारी प्रोग्रामिंग गलतियाँ
Recovery पहले से अनुमान हो तो अक्सर रिकवर हो सकता है आम तौर पर रिकवर नहीं होता
Recovery mechanism try/catch या throws बेहतर है सावधानी से कोड करें ताकि टलें
Example exception File not found Array index out of bounds
Java में डेटा टाइप्स और Exceptions

Checked और Runtime exceptions

  • Exception की वे subclasses जो RuntimeException की subclasses नहीं हैं, checked exceptions कहलाती हैं
    • इन्हें ज़रूर हैंडल करना पड़ता है
    • उदाहरण: file not found (FileNotFoundException)
  • RuntimeException सभी unchecked exceptions की superclass है
    • हैंडलिंग आवश्यक नहीं
    • उदाहरण: मौजूद न होने वाले array index को एक्सेस करना (IndexOutOfBoundsException)
    • उदाहरण: शून्य से भाग देना (ArithmeticException)
Java में डेटा टाइप्स और Exceptions

RuntimeException

RuntimeException Subclass कब थ्रो होती है उसका वर्णन
ArithmeticException गलत गणना, जैसे 0 से भाग देना
IndexOutOfBoundsException array, String आदि पर दिया गया index सीमा से बाहर
NegativeArraySizeException निगेटिव size वाला array बनाने की कोशिश
int y = 5/0; // ArithmeticException पैदा करने वाला कोड
int[] list = new int[-1];  //NegativeArraySizeException पैदा करने वाला कोड
Exception in thread "main" java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.NegativeArraySizeException: -1
1 See https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/RuntimeException.html
Java में डेटा टाइप्स और Exceptions

"Checked" Exception

public class LoadClass {
    public static void main(String[] args) {
        Class myClass = Class.forName("com.mysql.Driver");
    }
}
LoadClass.java:3: error: unreported exception ClassNotFoundException; 
must be caught or declared to be thrown
                Class myClass = Class.forName("com.mysql.Driver");
                                             ^
1 error
1 See https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Exception.html
Java में डेटा टाइप्स और Exceptions

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

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

Preparing Video For Download...