V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
esolve
V2EX  ›  问与答

Java 里的 nested exception 如何理解?

  •  
  •   esolve · 2017-10-20 11:41:42 +08:00 · 8546 次点击
    这是一个创建于 2395 天前的主题,其中的信息可能已经有所发展或是发生改变。

    经常看到,但是不理解什么意思

    比如这个:

    2017-10-20 11:38:11,211 ERROR - Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "x" [com.eversec.centertool.exception.GlobalControllerExceptionHandler.handleException(GlobalControllerExceptionHandler.java:37)] org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "x"

    有两个异常 NumberFormatException 和 MethodArgumentTypeMismatchException 如果我写一个捕捉语句 对两个异常都捕捉 哪个会优先被捕捉?

    3 条回复    2017-10-20 13:16:41 +08:00
    GeruzoniAnsasu
        1
    GeruzoniAnsasu  
       2017-10-20 12:05:31 +08:00
    嵌套异常不就是在异常处理中抛了新的异常
    如果在 try catch 外再捕获异常,应该只会捕获 catch 里抛出的新异常,旧的异常已经被 catch 处理了在外面捕获不到

    如果你想处理嵌套的异常之后还继续处理之前的异常,应该在 catch 里套一个 try catch
    GuuJiang
        2
    GuuJiang  
       2017-10-20 12:07:05 +08:00 via iPhone
    并没有抛出两个异常,你只能捕捉到 MethodArgumentTypeMismatchException,通过它的 getCause 方法能获得 NumberFormatException
    esolve
        3
    esolve  
    OP
       2017-10-20 13:16:41 +08:00
    @GuuJiang 你意思 spring 是先捕捉了 NumberFormatException,然后再抛出 MethodArgumentTypeMismatchException ?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   969 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:46 · PVG 04:46 · LAX 13:46 · JFK 16:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.