Java में डेटा टाइप्स और Exceptions
Jim White
Java Developer
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)
| 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 |
Exception की वे subclasses जो RuntimeException की subclasses नहीं हैं, checked exceptions कहलाती हैंFileNotFoundException)RuntimeException सभी unchecked exceptions की superclass हैIndexOutOfBoundsException)ArithmeticException)| 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
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
Java में डेटा टाइप्स और Exceptions