【问题标题】:Titan: NullPointerException is thrown instead of IllegalArgumentExceptionTitan:抛出 NullPointerException 而不是 IllegalArgumentException
【发布时间】:2014-04-26 03:39:26
【问题描述】:

在这段代码中

Vertex page            = graph.getVertex(pageId);

如果pageId 不存在,它应该抛出IllegalArgumentException,而是抛出NullPointerException。这里pagenull

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

【问题讨论】:

  • 也许graphnull,或者getVertex 不做null 检查。
  • 无图不为空。如果顶点不存在,则无需检查null Titan throws IllegalArgumentException

标签: titan


【解决方案1】:

如果 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

这是所有蓝图 API 的预期行为。至于“为什么”它以这种方式工作......我认为IllegalArgumentException 不适合“未找到”的情况。归根结底,这在很大程度上只是一个设计决定,我们认为返回 null 比抛出异常以提供有关“存在”的反馈要好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-18
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多