本文共 3086 字,大约阅读时间需要 10 分钟。
4. ??
?????????????????????????????????????????????????????????????
4.1 ????
???????????????????????????????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????Java??????????
4.2 ??????
Java????????????????????????Throwable??Throwable?????????
-
Error???????????????????????????????????????????????????????
-
Exception???????????????????????????????
Exception?????????-
RuntimeException??????????????????????????????????????????????
NullPointerException????????IndexOutOfBoundsException???????ArithmeticException?????????? -
Error?????????????????????
-
??????????????????????????????????????????????????????????
4.3 Exception???
???????????
??????Runtime Exception??????????????????????
NullPointerException???? null ????????????IndexOutOfBoundsException???????????????????ArithmeticException??????????????
????????????????????????
?????Non-Runtime Exception????????????????????
ParseException??????????????????
?????????????????????????
4.4 ????
Java?????????????????????????
- RuntimeException
- ClassCastException????????????????????
- ArrayIndexOutOfBoundsException???????????????????
- NullPointerException???? null ????????????
- ArithmeticException
- IOException????????????????????????
- FileNotFoundException????????????
- EOFException???????????
- ClassNotFoundException??????????
- InterruptedException????????????
- SQLException????????????
?????????????????????????????
4.5 ????
????????????????Java????????????????
4.5.1 ????
??????????
try { // ????????? // ?????????????????catch?} catch (ExceptionType1 e1) { // ??ExceptionType1?????} catch (ExceptionType2 e2) { // ??ExceptionType2?????} finally { // ???????} ?????
try???????catch??- ??
try???????????finally??????? finally???????????? cleanup ???
???
public static void main(String[] args) { try { int[] arr = new int[3]; System.out.println(arr[0]); System.out.println(arr[3]); // ?????ArrayIndexOutOfBoundsException System.out.println("over"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("??????" + e.getMessage()); } finally { System.out.println("?????"); }} 4.5.2 ??????
????????????????????????????????throws?????????
public void method() throws IOException { // ????IOException???} ?????
- ?????????????????????????????????????
- ???????????????????????????????
4.5.3 ?????????
?Java??Throwable????????????????
getMessage()??????????????toString()?????????????????printStackTrace()?????????????????????
4.6 ?????
????????????????????????????????????Exception?RuntimeException????
public class NoAgeException extends Exception { public NoAgeException(String message) { super(message); }}public class Person { private String name; private int age; public Person(String name, int age) throws NoAgeException { if (age < 0 || age > 200) { throw new NoAgeException("??????" + age); } this.name = name; this.age = age; }} ?????????????????????????????????????
???????????????????????????
发表评论
最新留言
关于作者