Java学习----有风险的代码(异常)
Exception继承了Throwable,但是它本身是有异常类的父类。
RuntimeException:运行时异常
Exception->RuntimeException->NullPointException
Exception->RuntimeException->IndexOutOfBoundsException->ArrayIndexOfboundsException
public class Test { String x; public static void main(String[] args) { Test test = new Test(); try { System.out.println(test.x.length()); } catch (ArrayIndexOutOfBoundsException e) { test.x = "hello world"; System.out.println(test.x.length()); } catch (NullPointerException e) { // TODO: handle exception test.x = "hello world"; System.out.println(test.x.length()); } catch (Exception e) { // 必须放到最后 // TODO: handle exception e.printStackTrace(); } System.out.println("end"); } }
11
end