Sobes.tech
Junior — Middle+

Implementing exception handling when calculating the perimeter of shapes

livecode

Task condition

  1. Extend the GeometryLibrary interface to allow obtaining the perimeter of any geometric shape.
  2. Construct a custom exception type for situations where calculating the perimeter is impossible.
  3. Ensure the ability to catch and handle this exception from the calling code.
interface GeometryLibrary {
    double perimeter();
}

class SquareGeometryLibraryImpl implements GeometryLibrary {
    @Override
    public double perimeter() {
        return 0;
    }
}
public class JetbulbInterview {
    public static void main(String[] args) {
        GeometryLibrary shapeCalc = new SquareGeometryLibraryImpl();
        try {
            double perim = shapeCalc.perimeter();
        } catch (PerimeterCalculationException e) {
            // handle error
        }
    }
}