控制针异常属于运行时异常,虚拟机编译时是无法发现的,所以需要手动捕获。
工具/原料
IDEA或MyEclipse
方法/步骤
public static void main(String[] args){ try{ getNullException(); }catch (NullPointerException e){ System.out.println('捕捉到的异常:' + e.getMessage()); } } public static void getNullException(){ String s = null; try{ s = s.replace('a',''); }catch (Exception e){ throw new NullPointerException(); } }
上一篇:牙周病的原因是什么?
下一篇:炸酱面炸酱制作方法