????Java?е????????????????????????Java???????????????????????????Щ?????????????????????????????????????????????????λ?á????????????Java?????????????????????????????
???????????
????????

??????Java?г??????????????????????????????????????????????????????????????????????VM(?????)???????????????????????VM???????????(???????)??????????????????????????????????Java??????????????????????????????????????????????Java?????е??????????????????Exception??
????????
?????????????????????ó??????????????????????????????????????????VM?????????(????????????κ??????????)???????????????????????????????(?????????)???????????Щ?????????????????? ???????????Java???????????????????????????????????????? ??????Java?????е?????????????????Error??
????????????????????????????????????????????????????????????????????????????????????????????????????
????1.??????????????????й????г???????????????ж???????????????
????2.???????????????????????????????????
?????????????
????1?????????(δ?????)
????2?????????(??????)
??????????????RuntimeException;?????????????????Java????Exception?????Error?и?????????Throwable??
????Error Exception
????runtimeException????????
????1?? java.lang.ArrayIndexOutOfBoundsException???????????????????????????????????????????????С??????
????2??java.lang.ArithmeticException??????????????磺??????????
????3??java.lang.NullPointerException
????????????????????????????????????????null??????????????磺????null??????????????????null??????????????null????????????throw??????null???
????4??java.lang.ClassNotFoundException
??????????????????????????????????????????????????????CLASSPAH????????????????class???????????????
??????????????
????try{}catch{}
????try{}catch{}finally{}??????????finally????鶼?????
????try{}finally{}????????????????catch{}finally{}??????
??????????й???У????????????????????????Χ????????????
???????????
???????????????????????????????????????????д?????????л???????(???????С????)???????????!
??????1. ?????????????????????????????????????????????????????
public static void testException1() {
int[] ints = new int[] { 1?? 2?? 3?? 4 };
System.out.println("???????");
try {
System.out.println(ints[4]);
System.out.println("?????????е???");// ???????????????????????
} catch (IndexOutOfBoundsException e) {
System.out.println("??????????");
}
System.out.println("???????");
}
/*output:
???????
??????????
???????
*/
public static void testException2() {
int[] ints = new int[] { 1?? 2?? 3?? 4 };
System.out.println("???????");
System.out.println(ints[4]);
System.out.println("?????????е???");// ?????????????????????????
}
????????????????е?????????IndexOutofBoundsException?????????????????????try…catch…????????????????????????????ò??????????????????????????????(???????????????????)???????????????????????????try?飬?????????????????????е??????????????????????????????????????????????????????????try…catch…?????”??????”????????????????????????????????????????????????????????????г??????????(???????????????????????????)??
??????2. ?????????
public class Rethrow {
public static void readFile(String file) throws FileNotFoundException {
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("???????δ????????????????????????????????????????????????????????????????????");
//?????????
throw e;
}
}
public static void printFile(String file) {
try {
readFile(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
printFile("D:/file");
}
}
??????????????????????????????????????????????????????С???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????ɡ???????????java??(?????????????)?????????????鷳????????????????????