当变量启动为null时,需要使构造函数崩溃

时间:2017-02-14 05:13:47

标签: java

例如,我有以下构造函数,我的一个要求是当构造函数为null时使构造函数崩溃。

public Test(String test){
    if(test != null){
        anyVariable = null;
    }
}

测试用例使用以下内容: catch(java.lang.Exception e)

1 个答案:

答案 0 :(得分:1)

当它为null时抛出异常:

if (test == null) {
    throw new Exception();
}

如果您需要留言,请执行以下操作:

if (test == null) {
    throw new Exception("test cannot be null");
}