美文网首页
Java异常的捕捉

Java异常的捕捉

作者: 133sheiya | 来源:发表于2017-04-25 11:20 被阅读6次

/**

  • 异常测试
  • @author bo

*/
public class Excep {

   public static void main(String[]args) {

    try {
    //要检查的程序语句;程序员感觉 到这句可能会出错
    int tempArr[] = new int[5];//声明一个存储五个int类型的数组
    tempArr [7]   = 10;
   } catch (ArrayIndexOutOfBoundsException e) {
    // TODO: handle exception
    //异常发生时的处理语句
    System.out.println("数组 超出了存储的范围!!!");
    System.out.println("异常的内容:"+e); //显示出异常的内容
   }finally {
    System.out.println("这里执行到了...");
    //一定会运行到的语句
    
                 }
     TestExp();

    System.out.println("main 函数 运行结束");

}
//抛出异常

      public static void TestExp(){
   //除数 不能为 0 

   int a = 4 ,b = 0;
    try {
     if (b == 0) {
         // throw 关键字所抛出的 是异常类的实例对象 因此必须使用new 产生对象
       throw new ArithmeticException("除数不能为0 出现算术异常");
    }else{
        System.out.println(a+"/"+b+"="+a/b);
    }
    
   } catch (Exception e) {
    // TODO: handle exception
    System.out.println("抛出的异常为:"+e);
    }
 
 
    }

}

相关文章

网友评论

      本文标题:Java异常的捕捉

      本文链接:https://www.haomeiwen.com/subject/hcqvzttx.html