- class Demo
- {
- int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException//在功能上通过throws关键字声明了该功能可能会出现的异常
- {
- int[] arr = new int[a];
- System.out.println(arr[4]);
- return a/b;
-
- }
- }
- class ExceptionDemo2
- {
- public static void main(String[] args) //throws Exception
- {
- Demo d = new Demo();
- try
- {
- int x = d.div(4,0);
- System.out.println("x="+x);
- }
-
-
- catch (ArithmeticException e)
- {
- System.out.println(e.toString());
- System.out.println("被零除了!!");
- }
- catch (ArrayIndexOutOfBoundsException e)
- {
- System.out.println(e.toString());
- System.out.println("角标越界啦!!");
- }
-
- /**/
- catch(Exception e)
- {
- System.out.println("hahah:"+e.toString());
- }
- System.out.println("over");
- }
- }
复制代码
明明是有除零了跟角标越界的,总是只能提示一条 |
|