[Big Data] Study Notes 1 Java SE Chapter 8 Exceptions 8.3 Custom Exceptions

[Big Data] Study Notes

insert image description here

1 Java SE

Chapter 8 Exceptions

8.3 Custom exceptions

Why do you need a custom exception class:

We said that different exception classes in Java represent a specific abnormal situation, so there are always some abnormal situations that are not defined in the core class library during development. At this time, we need to to define exception classes. For example, the problem of negative age, negative test scores and so on.

How exception classes are defined:

  1. Customize a compile-time exception type: define a class and inherit it java.lang.Exception.
  2. Customize a runtime exception type: define a class and inherit it java.lang.RuntimeException.

NoticeCustom exceptions can only be thrown through throw.

Demo custom exception:

package com.dingjiaxiong.define;

/**
 * @Projectname: BigDataStudy
 * @Classname: NotTriangleException
 * @Author: Ding Jiaxiong
 * @Date:2023/4/27 15:17
 */

public class NotTriangleException extends Exception {
    
    
    public NotTriangleException() {
    
    
    }

    public NotTriangleException(String message) {
    
    
        super(message);
    }
}
package com.dingjiaxiong.define;

/**
 * @Projectname: BigDataStudy
 * @Classname: Triangle
 * @Author: Ding Jiaxiong
 * @Date:2023/4/27 15:17
 */

public class Triangle {
    
    
    private double a;
    private double b;
    private double c;

    public Triangle(double a, double b, double c) throws NotTriangleException {
    
    
        if (a <= 0 || b <= 0 || c <= 0) {
    
    
            throw new NotTriangleException("三角形的边长必须是正数");
        }
        if (a + b <= c || b + c <= a || a + c <= b) {
    
    
            throw new NotTriangleException(a + "," + b + "," + c + "不能构造三角形,三角形任意两边之后必须大于第三边");
        }
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getA() {
    
    
        return a;
    }

    public void setA(double a) throws NotTriangleException {
    
    
        if (a <= 0) {
    
    
            throw new NotTriangleException("三角形的边长必须是正数");
        }
        if (a + b <= c || b + c <= a || a + c <= b) {
    
    
            throw new NotTriangleException(a + "," + b + "," + c + "不能构造三角形,三角形任意两边之后必须大于第三边");
        }
        this.a = a;
    }

    public double getB() {
    
    
        return b;
    }

    public void setB(double b) throws NotTriangleException {
    
    
        if (b <= 0) {
    
    
            throw new NotTriangleException("三角形的边长必须是正数");
        }
        if (a + b <= c || b + c <= a || a + c <= b) {
    
    
            throw new NotTriangleException(a + "," + b + "," + c + "不能构造三角形,三角形任意两边之后必须大于第三边");
        }
        this.b = b;
    }

    public double getC() {
    
    
        return c;
    }

    public void setC(double c) throws NotTriangleException {
    
    
        if (c <= 0) {
    
    
            throw new NotTriangleException("三角形的边长必须是正数");
        }
        if (a + b <= c || b + c <= a || a + c <= b) {
    
    
            throw new NotTriangleException(a + "," + b + "," + c + "不能构造三角形,三角形任意两边之后必须大于第三边");
        }
        this.c = c;
    }

    @Override
    public String toString() {
    
    
        return "Triangle{" +
                "a=" + a +
                ", b=" + b +
                ", c=" + c +
                '}';
    }
}

test class

package com.dingjiaxiong.define;

/**
 * @Projectname: BigDataStudy
 * @Classname: TestTriangle
 * @Author: Ding Jiaxiong
 * @Date:2023/4/27 15:17
 */

public class TestTriangle {
    
    
    public static void main(String[] args) {
    
    
        Triangle t = null;
        try {
    
    
            t = new Triangle(2, 2, 3);
            System.out.println("三角形创建成功:");
            System.out.println(t);
        } catch (NotTriangleException e) {
    
    
            System.err.println("三角形创建失败");
            e.printStackTrace();
        }

        try {
    
    
            if (t != null) {
    
    
                t.setA(1);
            }
            System.out.println("三角形边长修改成功");
        } catch (NotTriangleException e) {
    
    
            System.out.println("三角形边长修改失败");
            e.printStackTrace();
        }
    }
}

Test Results

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44226181/article/details/130480100