esolve
V2EX  ›  问与答

Java 里的 nested exception 如何理解?

  •  
  •   esolve · Oct 20, 2017 · 9094 views
    This topic created in 3139 days ago, the information mentioned may be changed or developed.

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

    比如这个:

    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 replies    2017-10-20 13:16:41 +08:00
    GeruzoniAnsasu
        1
    GeruzoniAnsasu  
       Oct 20, 2017
    嵌套异常不就是在异常处理中抛了新的异常
    如果在 try catch 外再捕获异常,应该只会捕获 catch 里抛出的新异常,旧的异常已经被 catch 处理了在外面捕获不到

    如果你想处理嵌套的异常之后还继续处理之前的异常,应该在 catch 里套一个 try catch
    GuuJiang
        2
    GuuJiang  
       Oct 20, 2017 via iPhone
    并没有抛出两个异常,你只能捕捉到 MethodArgumentTypeMismatchException,通过它的 getCause 方法能获得 NumberFormatException
    esolve
        3
    esolve  
    OP
       Oct 20, 2017
    @GuuJiang 你意思 spring 是先捕捉了 NumberFormatException,然后再抛出 MethodArgumentTypeMismatchException ?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1121 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 17:59 · PVG 01:59 · LAX 10:59 · JFK 13:59
    ♥ Do have faith in what you're doing.