zoukankan      html  css  js  c++  java
  • Java日志第35天 2020.8.9

    异常的注意事项

    1.多个异常分别处理

    import java.util.ArrayList;
    import java.util.List;
    
    public class Demo02Exception {
        //多个异常分别处理
        public static void main(String[] args) {
    
           ArrayList<Integer> list = new ArrayList<Integer>();
           list.add(1);
           list.add(2);
           list.add(3);
    
           int[] arr = {1, 2, 3};
    
           try{
               System.out.println(arr[3]);
           }catch (ArrayIndexOutOfBoundsException r){
               System.out.println(r);
           }
           
           try{
               System.out.println(list.get(3));
           }catch(IndexOutOfBoundsException e){
               System.out.println(e);
           }
        
        }

    2.多个异常一次捕获,多次处理

    import java.util.ArrayList;
    import java.util.List;
    
    public class Demo02Exception {
        public static void main(String[] args) {
    
           ArrayList<Integer> list = new ArrayList<Integer>();
           list.add(1);
           list.add(2);
           list.add(3);
    
           int[] arr = {1, 2, 3};
    
           try{
               System.out.println(arr[3]);
               System.out.println(list.get(3));
           }catch (ArrayIndexOutOfBoundsException r){
               System.out.println(r);
           } catch(IndexOutOfBoundsException e){
               System.out.println(e);
           }
    
        }
    }

    注意:

    catch里边定义的异常变量,如果有子父类关系,则子类的的异常变量必须写在上边

    3.多个异常一次捕获,一次处理

    import java.util.ArrayList;
    import java.util.List;
    
    public class Demo02Exception {
        public static void main(String[] args) {
    
           ArrayList<Integer> list = new ArrayList<Integer>();
           list.add(1);
           list.add(2);
           list.add(3);
    
           int[] arr = {1, 2, 3};
    
           try{
               System.out.println(arr[3]);
               System.out.println(list.get(3));
           }catch (ArrayIndexOutOfBoundsException r){
               System.out.println(r);
           } 
    
        }
    }

    *运行时异常被抛出可以不处理。既不捕获也不声明抛出

    *如果finally中有return语句,永远返回finally中的结果,避免该情况

    public class Demo03Exception {
    
        public static void main(String[] args) {
            int a = getA();
            System.out.println(a);
        }
    
        public static int getA() {
            int a = 10;
            try{
                return a;
            }catch (Exception e){
                System.out.println(e);
            }finally {
                a = 100;
                return a;
            }
        }
    }

     *如果父类抛出了多个异常,子类重写父类方法时,抛出和父类相同的异常或者是父类异常的子类或者不抛出异常。

    *父类方法没有抛出异常,子类重写父类该方法时也不可抛出异常。此时子类产生该异常,只能捕获处理,不能声明抛出。

    public class Fu {
        public void method01() throws NullPointerException, ClassCastException{
    
        }
    
        public void method02() throws IndexOutOfBoundsException {
    
        }
    
        public void method03() throws IndexOutOfBoundsException {
    
        }
    
        public void method04() {
    
        }
    
    }
    
    class Zi extends Fu{
        //子类重写父类方法时,抛出和父类相同的异常
        public void method01() throws NullPointerException, ClassCastException {}
        //子类重写父类方法时,抛出父类异常的子类
        public void method02() throws ArrayIndexOutOfBoundsException {}
        //子类重写父类方法时,不抛出异常
        public void method03() {}
    
        /*
            父类方法没有抛出异常,子类重写父类该方法时也不可抛出异常。
            此时子类产生该异常,只能捕获处理,不能声明抛出。
         */
        public void method04(){
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    自定义异常类

    格式

    public XXXException extends Exception | RuntimeException{

      添加一个空参数的构造方法

      添加一个有异常信息的构造方法

    }

    注意:

    1.自定义异常类一般都以Exception结尾,说明该类是一个异常类

    2.自定义异常类必须继承Exception或者RuntimeException

    继承Exception:说明该异常是编译期异常

    继承RuntimeException:说明该异常是运行期异常

    /*
    通过源码发现,所有的异常类都会有一个带有异常信息的构造方法
    方法内部调用父类异常信息的构造方法,让父类来处理这个异常信息
    同理,无参构造函数也可以通过父类进行处理
     */
    public class RegisterException extends Exception{
    
        //无参的构造方法
        public RegisterException() {
            super();
        }
    
        //含参的构造方法
        public RegisterException(String s) {
            super(s);
        }
    }

    练习

    模拟注册操作,如果用户名已存在,抛出异常并提示

    import java.util.Scanner;
    
    public class Demo01RegisterException {
    
        static String[] usernames = {"张三", "李四", "王五"};
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入将要注册的用户姓名:");
            String username = sc.next();
    
            check(username);
        }
    
        public static void check(String username) {
            for (String s : usernames) {
                if(s.equals(username)){
                    try {
                        throw new RegisterException("亲,该用户名已经被注册!");
                    } catch (RegisterException e) {
                        e.printStackTrace();
                        return;//结束
                    }
                }
            }
    
            System.out.println("亲,恭喜您注册成功!");
        }
    }

    结果如下:

    问题:

    父类方法没有抛出异常,子类重写父类该方法时也不可抛出异常。此时子类产生该异常,只能捕获处理,不能声明抛出。

     

    为什么并不会报错?

    明日任务:学习IO流

  • 相关阅读:
    UML建模之时序图(Sequence Diagram)
    UML统一建模语UML2和EnterpriseArchitect
    FTP服务器的搭建
    Ubuntu下Apache重启错误:Could not reliably determine解决
    JSP的优势 和劣势 与php的比较
    [置顶] Ajax 初步学习总结
    pv ticketlock解决虚拟环境下的spinlock问题
    Tomcat从零开始(十)Loader
    HDU 4740 The Donkey of Gui Zhou (模拟)
    关于cvScalar的那些事
  • 原文地址:https://www.cnblogs.com/Gazikel/p/13460506.html
Copyright © 2011-2022 走看看