如何创建可抛出任何异常的一般函数?

时间:2017-01-20 09:27:25

标签: java exception

我有两个类(结构示例):

public class FormRectangleComponent extends Form {
    private class RectangleComponent extends Component {
        private int color;

        public RectangleComponent(int aColor) {
            color = aColor;
        }

        @Override
        public void paint(Graphics aGraphics) {
            aGraphics.setColor(color);
            aGraphics.drawRect(getX(), getY(), getWidth(), getHeight());
            Map<String,Object> map = new LinkedHashMap<String,Object>() {{
                put("Display.displayWidth", Display.getInstance().getDisplayWidth());
                put("rectangleComponent.x", getX());
                put("rectangleComponent.y", getY());
                put("rectangleComponent.width", getWidth());
                put("rectangleComponent.height", getHeight());
            }};
            aGraphics.setColor(0xffffff);
            aGraphics.drawRect(getX() + 2, getY() + 2, 10, 10);
            report(map);
        }

        @Override
        protected Dimension calcPreferredSize() {
            int side = Display.getInstance().getDisplayHeight() / 10;
            return new Dimension(side, side);
        }
    }

    public FormRectangleComponent() {
        Display.getInstance().areMutableImagesFast();
        setTitle("FormRectangleComponent");
        setScrollable(false);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        contentPane.setScrollableY(true);
        contentPane.getAllStyles().setBgPainter((Graphics aGraphics, Rectangle aRectangle) -> {
            aGraphics.setColor(0x00c0c0);
            aGraphics.fillRect(aRectangle.getX(), aRectangle.getY(), aRectangle.getWidth(), aRectangle.getHeight());
        });
        contentPane.add(new SpanLabel(
                "Below should be some frames showing all sides."));
        contentPane.add(new RectangleComponent(0xff0000));
        contentPane.add(new RectangleComponent(0x0000ff));
        contentPane.add(new RectangleComponent(0x00ff00));
    }

    private void report(Map<String, Object> map) {
        int maximumLength = 0;
        for (Entry<String, Object> entry: map.entrySet()) {
            maximumLength = Math.max(maximumLength, entry.getKey().length());
        }
        for (Entry<String, Object> entry: map.entrySet()) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int tally = 0; tally < (maximumLength - entry.getKey().length()); tally ++) {
                stringBuilder.append(' ');
            }
            stringBuilder
            .append(entry.getKey())
            .append(": ")
            .append(entry.getValue());
            Log.p(stringBuilder.toString());
        }
    }
}

&#34; doCheck&#34;方法具有相同的逻辑来处理东西,但有一个不同的异常可以被每个类抛出:A类的A1Exception和B类的B1Exception

我的问题是: 我如何编写基本类来实现&#34; doCheck&#34;使用通用逻辑+在某些条件下应该抛出异常类? A和B应该扩展这个类。

1 个答案:

答案 0 :(得分:5)

您可以创建一个通用抽象类,将异常声明为泛型类型,然后创建一个必须在子类中重写的供应商方法。

public abstract class Checkable<X extends Exception> {

    protected abstract X getException(int stuff);

    protected void doCheck(int stuff) throws CommonException, X {
        if (stuff == 1) {
            throw this.getException(stuff);
        }
        throw new CommonException(stuff);
    }

}

子类看起来像:

public class A extends Checkable<A1Exception> {

    @Override
    protected A1Exception getException(int stuff) {
        return new A1Exception(stuff);
    }

    public void invokeStuff(int stuff) {
        doCheck(stuff);
    }

}

Ideone Demo