Titan:抛出NullPointerException而不是IllegalArgumentException

时间:2014-04-26 03:39:26

标签: titan

在此代码中

Vertex page            = graph.getVertex(pageId);

如果pageId不存在,则应该抛出IllegalArgumentException,而是抛出NullPointerException。此处pagenull

我正在捕捉IllegalArgumentException异常,但它从未捕获过。为什么呢?

1 个答案:

答案 0 :(得分:0)

如果pageId为null,则标准蓝图行为是抛出此测试强制执行的IllegalArgumentException

https://github.com/tinkerpop/blueprints/blob/2.5.0/blueprints-test/src/main/java/com/tinkerpop/blueprints/VertexTestSuite.java#L82

那就是说,我认为你在谈论顶点存在(正如你写的那样“如果pageId不存在的话。)在存在的情况下,标准蓝图行为是返回null如果标识符没有找到它的顶点。此行为由此测试强制执行:

https://github.com/tinkerpop/blueprints/blob/2.5.0/blueprints-test/src/main/java/com/tinkerpop/blueprints/VertexTestSuite.java#L157

正如您在REPL会话中看到的那样,Titan在测试时按预期工作:

gremlin> g = TitanFactory.open('conf/titan-cassandra.properties')
==>titangraph[cassandrathrift:127.0.0.1]
gremlin> g.getVertex(4)
==>v[4]
gremlin> g.getVertex(8)
==>null
gremlin> g.getVertex(null)
Vertex id can not be null
Display stack trace? [yN] y
java.lang.IllegalArgumentException: Vertex id can not be null

这是Blueprints API给出的所有预期行为。至于“为什么”它以这种方式工作......我认为IllegalArgumentException不适合“未找到”的情况。在一天结束时,这主要只是一个设计决策,我们认为返回null比抛出异常以提供有关“存在”的反馈要好。