Java基础---异常---练习

时间:2023-02-16 17:46:44
package com.train;
import java.lang.Math;
/********************
 * java.lang.Object
        java.lang.Math
 * static double sqrt(double a)---返回正确舍入的 double 值的正平方根。 
 * ******************/


class NoSanjiaoException extends RuntimeException{
    NoSanjiaoException(String msg) {
        super(msg);
    }
}

public class ExceptionTest {

    public static void main(String[] args) {
        
        Sanj sanj = new Sanj(10,20,30);
        
        System.out.println("This Sanj' area is  "+sanj.getArea());
        sanj.showInfo();
    }
}

class Sanj{
    private double x = 0;
    private double y = 0;
    private double z = 0;
    private double cirLength = 0;
    
    Sanj(double argX ,double argY, double argZ) throws NoSanjiaoException{
        this.x = argX;
        this.y = argY;
        this.z = argZ;
        this.cirLength = this.x + this.y +this.z;
        if(x+y<=z||x+z<=y||y+z<=x){
            throw new NoSanjiaoException("此三边无法构成三角形!");
        }
    }
    
    public void showInfo(){
        System.out.println("Sanj' x = "+this.x+"; y = "+this.y+"; z = "+this.z);
    }
    
    public double getArea(){
        return Math.sqrt(cirLength*(cirLength-x)*(cirLength-y)*(cirLength-z));
    }
}

:console:

Java基础---异常---练习