zoukankan      html  css  js  c++  java
  • java.lang中的异常类2020.12.18

    每日心得:

    今天比昨天好多了,老师花了大半个上午讲了那个创建一个MyStringBuffer的类的作业,说实话昨天没做多少,主要不知道方式,今天讲的时候也没全部听全,不过知道了答题的方式,逻辑理的还行吧。今天主要讲了java.lang中的异常类。

    1、error:用于指示合理的应用程序不应该试图捕获的严重问题;一般出了这个,那就不是我能够解决的了,现在暂时不用关心这个;

    2、exception:它指出了合理的应用程序想要捕获的条件。异常属于线程里面,发生会终止线程,有传播性,不管则会根据层数逐渐传播到最顶层jvm会将其终止;

    异常抛出:方式throws与try-catch-finally,抛出会有三种事物,一个快照(类似电脑休眠,保持计算机原来的状态),一个详细信息(message),一个缘由(cause),finally中的语句一定会执行,如果在前方有语句强制结束了程序,它会在结束前执行,一般用于资源释放。还有部分知识点在下图:

     

    异常又分为checked与runtime

    runtime异常与error类似,系统会忽略,因为人为难以解决,父子子类抛出时没有联系,
    check异常必须处理,父类抛出的异常类必须大于或等于子类抛出的异常,
    但两者都可使线程结束,效果相同,设计原因不明。
    java框架中会将check都会转成runtime异常,在编程时不用处理,但不是不处理,会在某工具里集中处理,
    自定义异常,java中的异常太大或太小,或者有些情况没概况到有必要设计自己的异常,
    再有可以有必要让最顶层使用的人是否知道其异常的原因,或者自己解决不让使用者知道。(这个在架构师时才会想到这么多。我们一般不用处理)。

    今天还有一个作业,做一个学生管理系统,按照要求要把异常都处理好。20号终于做完了,每一种非法输入都得想到,还真是挺麻烦的,一开始直接用student[]数组做的,麻烦很多,长度固定,让我只能给一个长度,在使用equals作部分判断时,因为要循环,后面的null值不可避免会遍历到,所以老是导致空指针错误,后面直接使用List方便很多,长度可变,有序性的确让题目好做不少,至少我在把类数组转换为list的时候删去了很多不必要的代码,做这个题目让我得到很多技巧,比如将多次使用的功能放在一个方法内,这样会很大程度上减少代码重复度,更加简洁,也有利于性能,每次错误输入都需要重新再次输入,反复调用相应的方法,老师说系统做完的时间与优化的时间不能差太多,我这优化做的时间太多了,下次还是得争取在做的时候将更多情况考虑到!

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    
    public class StudentManager {
        public static void main(String[] args) throws Exception {
            Student student1=new Student("作者","Qifan_12345678",18,true);
            Student student2=new Student("读者","Qifan_23456789",19,false);
            stus.add(student1);
            stus.add(student2);
            system();
        }
        static BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        static List<Student> stus = new ArrayList<StudentManager.Student>();
        static String regex = "[u4E00-u9FA5]+";
        static String regex2 = "^[0-9]*+";
        static class Student{
            private String name;//中文,且不超过5个字
            private String stuNo;//Qifan_8个数字
            private int age;// 18-25之间
            private boolean gender;
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public String getStuNo() {
                return stuNo;
            }
            public void setStuNo(String stuNo) {
                this.stuNo = stuNo;
            }
            public int getAge() {
                return age;
            }
            public void setAge(int age) {
                this.age = age;
            }
            public boolean isGender() {
                return gender;
            }
            public void setGender(boolean gender) {
                this.gender = gender;
            }
            public Student(String name,String stuNo,int age,boolean gender) {
                this.name = name;
                this.stuNo = stuNo;
                this.age = age;
                this.gender = gender;
            }
        }
        static void system() {
            stuhe();
            try {
                String Str = reader.readLine();
                int key =Integer.parseInt(Str);
                if(key>0&&key<5){
                        switch (key) {
                        case 1:
                            addstu();
                            break;
                        case 2:
                            deletestu();    
                            break;
                        case 3:
                            updatestu();
                            break;
                        case 4:
                            querystu();
                            isqureystu();
                            break;
                        case 5:
                            System.out.println("退出系统成功!");
                            System.exit(0);
                            break;
                        }
                }else{
                    System.out.println("");
                    System.out.println("请输入1~5的功能数字!");
                    system();
                }
            } catch (Exception e) {
                System.out.println("");
                System.out.println("请输入功能数字!");
                system();
            }    
        }
        static void stuhe(){
            System.out.println("欢迎进入学生管理系统!");
            System.out.println("1、添加学生");
            System.out.println("2、删除学生");
            System.out.println("3、修改学生");
            System.out.println("4、查询学生");
            System.out.println("5、退出系统");
            System.out.print("请选择功能输入数字>>>>>");
        }
        static String inname() throws IOException{
            System.out.println("请输入姓名:");
            String sname = reader.readLine();
            if(sname.length()<0||sname.length()>6||!(sname.matches(regex))){
                System.out.println("姓名格式为五个字以内的中文!");
                sname=inname();
            }
            return sname;
        }
        
        static String inno() throws IOException{
            System.out.println("请输入学号:");
            String sno = reader.readLine();
            if(sno.length()!=8||!(sno.matches(regex2))){
                System.out.println("学号格式为8个数字以内!");
                sno =inno();
            }
            return sno;
        }
        
        static int inage() throws IOException{
            System.out.println("请输入需要添加的年龄:");
            String sage = reader.readLine();
            int iage=0;
            if(sage.length()==2&&(sage.matches(regex2))){
                iage=Integer.valueOf(sage);
                if(iage<18&&iage>25){
                System.out.println("年龄为18到25岁!");
                iage =inage();
                }else{
                    iage=Integer.valueOf(sage);    
                }
            }else{
                System.out.println("年龄为18到25岁!");
                iage =inage();
            }
            return iage;
        }
        
        static boolean ingen() throws IOException{
            System.out.println("请输入需要添加的性别:");
            String gender = reader.readLine();
            boolean b=true;
            if(gender.equals("男")){
                gender="true";
                b=Boolean.valueOf(gender);
            }else if(gender.equals("女")){
                gender="false";
                b=Boolean.valueOf(gender);
            }else{
                System.out.println("请输入性别(男/女):");
                b=ingen();
            }
            return b;
        }
        static void yn(int i) throws IOException{
            switch (i) {
            case 1:
                System.out.println("输入y继续添加,n退回主菜单");
                break;
            case 2:
                System.out.println("输入y继续删除,n退回主菜单");    
                break;
            case 3:
                System.out.println("输入y继续修改,n退回主菜单");
                break;
                }
            String key = reader.readLine();
            if(key.equals("y")){
                switch (i) {
                case 1:
                    addstu();
                    break;
                case 2:
                    deletestu();    
                    break;
                case 3:
                    updatestu();
                    break;
                }
            }else if(key.equals("n")){
                system();
            }else{
                    System.out.println("请输入y/n!");
                    yn(i);
                }
        }
        static void addstu() throws IOException {
            querystu();
            String sname=inname();
            String sno = inno();
            for(int i=0;i<stus.size();i++){
                if((stus.get(i)).getStuNo().equals("Qifan_"+sno)){
                    System.out.println("该学号已存在!,请重新输入!");
                    sno=inno();
                }
            }
            int sage = inage();
            boolean b = ingen();
            Student s1=new Student(sname,"Qifan_"+sno,sage,b);
            stus.add(s1);
            querystu();
            System.out.println("添加成功!");
            yn(1);
        }
        static void deletestu()throws IOException{
            querystu();
            System.out.println("请输入需要删除的学号或姓名:");
            String s = reader.readLine();
            String text="";
            for(int i=0;i<stus.size();i++){
                if((stus.get(i).getName()).equals(s)||(stus.get(i).getStuNo()).equals("Qifan_"+s)){
                    System.out.println("确认删除?y/n");
                    String key1 = reader.readLine();
                    if(key1.equals("y")){
                        stus.remove(i);
                        querystu();
                        text="删除成功!";
                    }else if(key1.equals("n")){
                        text="已取消删除!";
                    }    
                    break;
                }else{
                    text="没有此人,删除失败!";
                }
            }
            System.out.println(text);
            yn(2);
                    
        }
        static void updatestu()throws IOException{
            querystu();
            String s = inno();
            String text="";
            for(int i=0;i<stus.size();i++){
                    if((stus.get(i)).getStuNo().equals("Qifan_"+s)){
                        String n = inname();
                        System.out.println("确认修改?y/n");
                        String key1 = reader.readLine(); 
                            if(key1.equals("y")){
                                stus.get(i).setName(n);
                                querystu();
                                text="修改成功!";
                            }else if(key1.equals("n")){
                                text="已取消修改!";
                            }    
                            break;
                    }else{
                    text="没有此人,修改失败!";
                }
            }
            System.out.println(text);
            yn(3);
        }
        static void querystu(){
            System.out.println("学生列表:");
            for(int i=0;i<stus.size();i++){
                if(stus.get(i)!=null){
                    System.out.println(stus.get(i));
                }
            }
        
        }
        static void isqureystu() throws IOException{
            System.out.println("输入y进行精准查询,n退回主菜单");
            String key = reader.readLine();
            if(key.equals("y")){
                String sname = inname();
                for(int i =0;i<stus.size();i++){
                    if(stus.get(i)!=null&&(stus.get(i).getName()).equals(sname)){
                        System.out.println(stus.get(i));
                    }else{
                        System.out.println("查无此人!");
                    }
                    isqureystu();
                }
            }else if(key.equals("n")){
                system();
                
            }else{
                System.out.println("请输入y/n!");
                isqureystu();
            }
        }
    }
  • 相关阅读:
    物联网与边缘计算的融合
    在【自我认知】大学,你可能永远毕不了业
    Spring Security实现短信验证码登录
    线上课程
    【技术人成长】公众号
    大数据是阿猫阿狗都能玩的吗
    机器不能代替你思考
    如何缓解需求沟通中的鸡同鸭讲
    如何成为一个更渊博的技术人
    招聘季,聊聊那些古怪的候选人
  • 原文地址:https://www.cnblogs.com/zzdbk/p/14157597.html
Copyright © 2011-2022 走看看