多语言展示
当前在线:1559今日阅读:60今日分享:41

java空指针异常如何抛出

控制针异常属于运行时异常,虚拟机编译时是无法发现的,所以需要手动捕获。
工具/原料

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();     } }

推荐信息