您的位置:首页 > 其它

怎么用最简单的方法抛出一个手动的异常

2017-08-07 00:00 387 查看
摘要: 有些时候我们想测试代码出现异常后代码会如何执行,那么该如何故意抛出异常呢

try {

System.out.println(1 / 0);//会抛出ArithmeticException

System.out.println(new int[] {}[0]);//会抛出ArrayIndexOutOfBoundsException

String str = null;

System.out.println(str.toString());//会抛出NullPointerException

} catch (ArithmeticException e) {

resultMap.put("errMsg", e.getMessage());

System.out.println("算术异常");

System.out.println(e.getMessage());

return ERROR;

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("数组下标越界异常");

return ERROR;

} catch (NullPointerException e) {

System.out.println("空指针异常");

return ERROR;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐