您的位置:首页 > 职场人生

网易的一道面试题

2017-03-25 16:27 183 查看

编写一个Android程序在该程序中抛出下列三种异常:

刚刚做完网易的一道实习生笔试题,大致题目意思是,编写一个Android程序让后抛出5个类型的异常,具体不记得了,下面百度了一道差不多意思的题,总共有三种异常,记录一下:

题目:

编写一个程序在该程序中抛出下列三种异常,ArithmeticException、ArrayIndexOutOfBoundsException和NullPointerException,并分别用三个catch语句捕获这三个异常对象,分别输出以下字符串:“算术异常”,“数组下标越界异常”和“空指针异常”。

public class A {
public static void main(String[] args) {
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) {
System.out.println("算术异常");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界异常");
} catch (NullPointerException e) {
System.out.println("空指针异常");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  面试题 android 网易