java异常1

java的非正常情况分为两种 Exception 和error  error 不能捕获和恢复 这不是程序员要关心的

重要的异常的处理和捕获 常见的异常有空指针异常 数组越界异常  除零异常 类型转换异常等等

 

public class DivTest
{
 public static void main(String[] args)
 {
  try
  {
   int a = Integer.parseInt(args[0]);
   int b = Integer.parseInt(args[1]);
   int c = a / b;
   System.out.println("您输入的两个数相除的结果是:" + c );
  }
  catch (IndexOutOfBoundsException ie)
  {
   System.out.println("数组越界:运行程序时输入的参数个数不够");
  }
  catch (NumberFormatException ne)
  {
   System.out.println("数字格式异常:程序只能接受整数参数");
  }
  catch (ArithmeticException ae)
  {
   System.out.println("算术异常");
  }
  catch (Exception e)
  {
   System.out.println("未知异常");
  }
 }
}
异常的处理的过程应该是先处理小异常然后是大异常   所以父类异常catch块放在后面

你可能感兴趣的:(java异常1)