Junior — Middle+
Implementing exception handling when calculating the perimeter of shapes
livecode
Task condition
- Extend the
GeometryLibraryinterface to allow obtaining the perimeter of any geometric shape. - Construct a custom exception type for situations where calculating the perimeter is impossible.
- 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
}
}
}