JAVA自定義異常類的一種實現方式


/*
*自定義異常類
*/
public class UnsupportedShapeException extends Exception{

    private static final long serialVersionUID = 1L;

    public UnsupportedShapeException(){
        System.out.println("我的自定義異常類");
    }

}

/*
*使用
*/

public class ShapeFactory {
    
    public static Shape createShape(String shape) throws UnsupportedShapeException{
        
        if(shape.equalsIgnoreCase("circle")){
            System.out.println("producing circle....");
            return new Circle();
        }else if(shape.equalsIgnoreCase("rectangle")){
            System.out.println("producing rectangle....");
            return new Rectangle();
        }else if(shape.equalsIgnoreCase("triangle")){
            System.out.println("producing triangle....");
            return new Triangle();
        }else{
            throw new UnsupportedShapeException();
        }

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章