• 《Java从入门到精通》第十一章学习笔记


    第11章 异常处理

    一、异常的分类

    1、可控式异常:

    (1)IOException:当发送某种I/O的异常;

    (2)SQLException:关于数据库访问或其他错误的异常

    (3)ClassNotFoundException:类没有找到的异常

    (4)NoSuchFieldException:类不包含指定名称的字段时的异常

    (5)NoSuchMethodException:无法找到某一方法时候的异常

     1 public class Ex1 {
     2     
     3     private int num=10;
     4     public int getNum(){
     5         return this.num;
     6     }
     7     public void setNum(int num){
     8         this.num=num;
     9     }
    10     public Ex1(){
    11         try{
    12             Class.forName("Ex123");   //尝试装载类
    13         }catch(ClassNotFoundException e){  //捕获异常
    14             e.printStackTrace();     //异常的输出方法
    15         }
    16         System.out.println("测试!");
    17     }
    18 
    19     public static void main(String[] args) {
    20         // TODO Auto-generated method stub
    21         Ex1 test=new Ex1();
    22         test.setNum(888);
    23         System.out.println(test.getNum());
    24     }
    25 }

    2、运行时异常
    (1)IndexOutOfBoundsException:集合或数组的索引超出范围的异常

    (2)NullPointerException:程序在需要对象的时候使用了null的异常

    (3)ArithmeticException:数学运算的异常

    (4)IllegalArgumentException:方法传递了非法的参数

    (5)ClassCastException:将对象强制换换成不是实例的子类的异常

     1 public class Exc2 {
     2     int[] number={100,80,50,70,20,60};
     3     public void setNum(int index,int value){
     4         number[index]=value;
     5     }
     6     public int getNum(int index){
     7         return number[index];
     8     }
     9     public static void main(String[] args) {
    10         Exc2 test=new Exc2();
    11         for(int i=0;i<=10;i++){
    12             try{
    13                 System.out.println(test.getNum(i));
    14                 System.out.println("第"+i+"个数组元素正常显示");
    15             }catch(Exception e){
    16                 e.printStackTrace();
    17                 System.out.println("第"+i+"个元素无法读取!");
    18                 break;
    19             }
    20         }
    21     }
    22 }

    二、处理异常
    对于程序中可能发生异常的语句,可以将其添加到try语句块中,并用catch语句块进行相应处理。有try-catch语句,和try-catch-finally语句。

    其中finally语句块中是无论try语句块中程序是否发生异常都会执行的代码。

    三、实战练习

     1 /**
     2  * 编写一个异常类MyException,再编写一个Student类,其中有一个产生异常的speak(int m)方法
     3  * 当m大于1000时,方法抛出MyException对象
     4  * 通过主类测试
     5  * @author TS
     6  */
     7 public class Test{
     8     public class MyException extends Exception{
     9         public MyException(int i){
    10             System.out.println("发生异常!i的值不能大于1000!");
    11             System.out.println("目前i值为"+i);
    12         }
    13     }
    14     public class Student{
    15         public void speak(int m) throws MyException{
    16             if(m>1000){
    17                 throw new MyException(m);
    18             }else{
    19                 System.out.println("未发现异常!");
    20             }
    21         }
    22     }
    23     public static void main(String[] args){
    24         Test test=new Test();
    25         Student stu=test.new Student();
    26         try{
    27             stu.speak(1001);
    28         }catch(MyException ex){
    29             ex.printStackTrace();
    30         }
    31     }
    32 }
     1 /**
     2  * 创建Number类,通过类的count()方法得到任意两个数相加的结果
     3  * 如果向count()方法传递负数,则抛出自定义异常
     4  * 在主类中测试
     5  * @author TS
     6  */
     7 public class Test {
     8     public class Number{
     9         long count(long m,long n) throws MyException{
    10             if((m<0)||(n<0)){
    11                 throw new MyException();
    12             }else{
    13                 System.out.println("正确返回!结果为"+(m+n));
    14                 return m+n;
    15             }
    16         }
    17     }
    18     public class MyException extends Exception{
    19         public MyException(){
    20             System.out.println("此处抛出自定义异常MyException!");
    21             System.out.println("提醒:加数中有负数!");
    22         }
    23     }
    24     public static void main(String[] args){
    25         Test test=new Test();
    26         Number num=test.new Number();
    27         try{
    28             num.count(12, -15);
    29         }catch(MyException e){
    30             e.printStackTrace();
    31         }
    32         
    33     }
    34 
    35 }
  • 相关阅读:
    毕设进度2020.02.06
    毕设进度2020.02.05
    毕设进度2020.02.04
    学习进度2020.02.03
    学习进度2020.02.02
    学习进度2020.02.01
    毕设之路:本周计划(五)2019.11.18——2019.11.23
    毕设之路:本周计划(四)2019.11.11——2019.11.17
    洛谷P2015 二叉苹果树
    洛谷P1131 [ZJOI2007]时态同步
  • 原文地址:https://www.cnblogs.com/tsembrace/p/4605412.html
Copyright © 2020-2023  润新知