Java异常解决

Java异常解决

温故确实可以知新

  1. ArithmeticException异常:一个整数“除以零”时

    public class Test1 {
        public static void main(String[] args) {
            int b=0;
            System.out.println(1/b);//
        }
    }
    //当我们定义的被除数为整型时(short、int、long)会抛出此异常,被除数为整型时不可为零。
    

    执行结果如图:

    www.zeeklog.com  - Java异常解决

    解决如上异常:

    public class Test1 {
        public static void main(String[] args) {
            int b=0;
            if(b!=0){
                System.out.println(1/b);
            }
        }
    }
    
  2. NullPointerException空指针异常:访问外界传进来为null

    public class Test2 {
        public static void main(String[] args) {
            String str = null;
            System.out.println(str.charAt(0));
        }
    }
    

    执行结果如图:

    www.zeeklog.com  - Java异常解决

    解决如上异常:

    public class Test2 {
        public static void main(String[] args) {
            String str=null;
            if(str!=null){
                System.out.println(str.charAt(0));
            }
        }
    }
    
  3. ClassCastException类型转换异常

    class Animal {
    }
    class Dog extends Animal {
    }
    class Cat extends Animal {
    }
    public class Test3 {
        public static void main(String[] args) {
            Animal a = new Dog();
            Cat c = (Cat) a;//子类转子类产生异常
        }
    }
    

    执行结果如图:

    www.zeeklog.com  - Java异常解决

    解决如上异常:

    public class Test3 {
        public static void main(String[] args) {
            Animal a = new Dog();
            if (a instanceof Cat) { //引用类型转换异常用关键字判断
                Cat c = (Cat) a;
            }
        }
    
  4. ArrayIndexOutOfBoundsException数组下标越界异常

    public class Test4 {
        public static void main(String[] args) {
            int[] arr = new int[5];
            System.out.println(arr[5]);
        }
    }
    

    解决如上异常:

    public class Test4 {
        public static void main(String[] args) {
            int[] arr = new int[5];
            int a = 5;
            if (a < arr.length) {  //增加关于边界的判断
                System.out.println(arr[a]);
            }
        }
    }
    
  5. NumberFormatException数字格式化异常

    public class Test5 {
        public static void main(String[] args) {
            String str = "1234abcf";
            System.out.println(Integer.parseInt(str));
        }
    }
    

    解决如上异常:

    public class Test5 {
        public static void main(String[] args) {
            String str = "1234abcf";
            Pattern p = Pattern.compile("^\\d+$"); //使用正则表达式解决
            Matcher m = p.matcher(str);
            if (m.matches()) { // 如果str匹配代表数字的正则表达式,才会转换
                System.out.println(Integer.parseInt(str));
            }
        }
    }
    

总结:健壮的代码都要做判断,避免异常

Read more

超快速,使用ChatGPT编写回归和分类算法

超快速,使用ChatGPT编写回归和分类算法

本文将使用一些 ChatGPT 提示,这些提示对于数据科学家在工作时非常重要。 微信搜索关注《Python学研大本营》,加入读者群,分享更多精彩 以下是一些示例ChatGPT 提示的列表以及数据科学家的响应。 ChatGPT 提示 为决策树回归算法生成 python 代码。 下面是使用scikit-learn在 Python 中进行决策树回归的示例代码: import numpy as np import matplotlib.pyplot as plt from sklearn.tree import DecisionTreeRegressor # Generate random data rng = np.random.default_rng() x = 5 * rng.random(100) y = np.sin(x) + 0.

By Ne0inhk
力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

力扣每日一题:993.二叉树的堂兄弟节点 深度优先算法

993.二叉树的堂兄弟节点 难度:简单 题目: 在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。 如果二叉树的两个节点深度相同,但 父节点不同 ,则它们是一对堂兄弟节点。 我们给出了具有唯一值的二叉树的根节点 root ,以及树中两个不同节点的值 x 和 y 。 只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true 。否则,返回 false。 示例: 示例 1: 输入:root = [1,2,3,4], x = 4, y = 3 输出:false

By Ne0inhk
1239.串联字符串的最大长度 关于字符串的回溯算法!

1239.串联字符串的最大长度 关于字符串的回溯算法!

题目: 给定一个字符串数组 arr,字符串 s 是将 arr 某一子序列字符串连接所得的字符串, 如果 s 中的每一个字符都只出现过一次,那么它就是一个可行解。 请返回所有可行解 s 中最长长度。 提示: 1 <= arr.length <= 16 1 <= arr[i].length <= 26 arr[i] 中只含有小写英文字母 示例: 示例 1: 输入:arr = ["un","iq","ue"] 输出:4 解释:所有可能的串联组合是

By Ne0inhk