如何为包含自定义消息的列表创建自定义异常处理程序?

时间:2016-04-16 12:07:44

标签: java unit-testing exception-handling

我需要创建一个整数列表,它接受字符串作为值并满足这些要求:

添加的值必须是数字的字符串表示。该列表应抛出自定义异常,并在以下情况下使用自定义消息:  *添加的值为null或为空  *添加的值不是数字的字符串表示形式  *我们尝试从列表中读取的索引超出范围

我还有一些单元测试,用于检查是否正确处理了异常。

这是CustomException类:

public class CustomException extends Exception {

    public CustomException(String message) {
        super(message);
    }


    public CustomException() {
    }


    public CustomException expect(NumberFormatException e) {
        return new CustomException("Not a number");
    }

    public CustomException expect(NullPointerException e) {
        return new CustomException("Null");
    }

    public CustomException expect(IndexOutOfBoundsException e) {
        return new CustomException("Index out of bounds");
    }
}

以下是单元测试:

public class MyListTest {

    private List<String> list;

    private Class<CustomException> exceptionType = CustomException.class;

    private String[] initData = {"12", "23", "34", "45"};

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Before
    public void setUp() {
        this.list = new StringList();
    }

    @Test
    public void testAddValuesToTheList() {
        initData();
        assertEquals(initData.length, list.size());
        for (String data : initData) {
            assertTrue(list.contains(data));
        }
    }

    private void initData() {
        for (String numberAsString : initData) {
            list.add(numberAsString);
        }
    }

    @Test
    public void testAddNonIntegerValue() {
        exception.expect(exceptionType);
        exception.expectMessage("Invalid number.");
        list.add("Hey, I'm not an integer.");
    }

    @Test
    public void testAddNonNullValue() {
        exception.expect(exceptionType);
        exception.expectMessage("Null");
        list.add(null);
    }

    @Test
    public void testIndexOutOfBounds() {
        initData();
        exception.expect(exceptionType);
        exception.expectMessage("Index out of bounds.");
        list.get(initData.length);
    }
}

所以我的问题是:如何使用自定义异常类来检查我的列表的上述要求?

1 个答案:

答案 0 :(得分:0)

 public void add(Object element) {
        if (element == null) {
            try {
                throw new CustomException("Null");
            } catch (CustomException e) {
                System.err.println("Null");
            }
        } else {
            list.add(Integer.valueOf((String) element));    
        } //how to handle NUMBER FORMAT EXCEPTION WITH MY CUSTOM EXCEPTIONS

    }

上面是您的代码,文件中的每个方法都是非常类似的错误,throwing out the exception自定义异常中的try catch block将是thrown,它将是handled 1}}在same time,如果您计划throwing exception,请执行此操作 另外更改您的其他methods

public void add(Object element) throws CustomException {
            if (element == null) {
                throw new CustomException("Null");
             } else {
                list.add(Integer.valueOf((String) element));    
            } //how to handle NUMBER FORMAT EXCEPTION WITH MY CUSTOM EXCEPTIONS

        }