访问 Java 异常对象的详细信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6503686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:07:45  来源:igfitidea点击:

Access Java Exception Objects's detailed message

javaexception-handling

提问by Sridevi Laks

I am debugging my application, I have added exception.getMessage()in logger, but exception.getMessage()prints Null, but , when I debug I could see my Exception String in exception object's detailed message, how can I get the exception message that is coming as detailed message? Note - getMessagereturns Null.

我正在调试我的应用程序,我已经exception.getMessage()在记录器中添加了,但是exception.getMessage()打印Null,但是,当我调试时,我可以在异常对象的详细消息中看到我的异常字符串,我怎样才能得到作为详细消息的异常消息?注意getMessage返回Null

P.S - I am not using PrintStackTrace or stacktraceElement, my logger should return the string from exception.getmessage(), that is the requirement.

PS - 我没有使用 PrintStackTrace 或 stacktraceElement,我的记录器应该从 exception.getmessage() 返回字符串,这是要求。



From comment:

来自评论:

DBException dbExe = new DBException(sqlExe); 
DBException objDbEx = (DBException) ExceptionUtil.populateSuperException(dbExe, strTraceMesg, ConstantsIF.SEVERE_LEVEL, false, null, null, null); 
throw objDbEx;


public static SuperException populateSuperException (SuperException exSuperException, String strTraceMsg, char chTraceLevel, ) {
  if (strTraceMsg != null) { 
    switch (chTraceLevel) { 
      case Con.IN: 
      case Con.S: 
         //log
    } 
  } 
  return exSuperException; 
}

回答by Giann

You can print out the full stack trace:

您可以打印出完整的堆栈跟踪:

 exception.printStackTrace();

回答by oliholz

Try:

尝试:

            switch (chTraceLevel) { 
                case Con.IN: 
                case Con.S: 
                    String msg = exSuperException.getCause().getMessage();
                    // LOG msg
            } 

回答by Andreas Dolk

That is pretty strange. The message that you see with debugging is usually created through Throwable#toStringand that calls getLocalizedMessage()internally.

这很奇怪。您在调试中看到的消息通常是通过创建Throwable#toString并在getLocalizedMessage()内部调用的。

So ifthe exception does have a message, thenit should be returned through getMessage()andbe part of the toString()result.

所以如果异常确实有消息,那么它应该通过返回getMessage()成为toString()结果的一部分。

An exception/throwable does not need to have a detailed message. Please double check, if you've created your exceptions with a message.

异常/抛出不需要有详细的消息。请仔细检查,如果您创建了带有消息的例外。



After making your code readable: your DBExceptionis created without a message, so dbExe.getMessage()will return null. Either look the the causeor add a message while creating:

使您的代码可读后:您的代码DBException创建时没有 message,因此dbExe.getMessage()将返回null. 要么查看原因,要么在创建时添加一条消息:

DBException dbExe = new DBException(sqlExe.toString(), sqlExe);// just an example

回答by Andrzej Doyle

You are creating your exception object without any message (you're only specifying the chained exception):

您正在创建没有任何消息的异常对象(您只指定了链式异常):

DBException dbExe = new DBException(sqlExe); 

hence calling dbExe.getMessage()may (correctly) return null (with the details depending on what your constructor does in this situation). The reason you can see a message in the stacktrace, incidentally, is because the generation of the stacktrace recurses to the underlying exception, and sqlExe.getMessage()will be what you're seeing.

因此调用dbExe.getMessage()可能(正确)返回 null(详细信息取决于您的构造函数在这种情况下的作用)。顺便说一下,您可以在堆栈跟踪中看到消息的原因是堆栈跟踪的生成递归到底层异常,并且sqlExe.getMessage()将是您所看到的。

The solution is simply to provide a message as well as the underlying exception when constructing your exception. The general wisdom is that this should reflect the level at which the exception is thrown, so perhaps something like the following:

解决方案只是在构造异常时提供消息以及底层异常。一般的观点是,这应该反映抛出异常的级别,因此可能类似于以下内容:

DBException dbExe = new DBException("Unable to persist widgets", sqlExe);

If your unspecified "requirement about the existing code flow" means that you need the actual database exception to be the message in dbExe, you could construct this like the following:

如果您未指定的“关于现有代码流的要求”意味着您需要实际的数据库异常作为 dbExe 中的消息,您可以像下面这样构造:

DBException dbExe = new DBException(sqlExe.getMessage(), sqlExe); 

though on the whole that duplication of the message isn't very nice, and the former option is the more "correct" one.

虽然总的来说,消息的重复不是很好,前一个选项是更“正确”的选项。

回答by maasg

From the discussion in the comments, it is my conclusion that the root cause of your problem is in the implementation of the DBExceptionconstructor or its hierarchy. For a valid reason or not, I think it's not calling the exception class hierarchy (e.g. super(reason);) and therefore, you are not getting the expected behaviour from the call to dbException.getMessage(). Note that a call to new Exception(anotherException)will always populate the field backing the getMessage()call in the base Throwable class through this call chain: (only relevant bits shown)

从评论中的讨论来看,我的结论是您的问题的根本原因在于DBException构造函数或其层次结构的实现。无论是否有正当理由,我认为它没有调用异常类层次结构(例如super(reason);),因此,您没有从对dbException.getMessage(). 请注意,调用new Exception(anotherException)将始终getMessage()通过此调用链填充支持基 Throwable 类中调用的字段:(仅显示相关位)

public Throwable(Throwable cause) { 
    ... 
    detailMessage = (cause==null ? null : cause.toString());
     ... 
} 
public String toString() { 
    ... 
    String message = getLocalizedMessage(); 
    ...
} 

public String getLocalizedMessage() { 
    return getMessage();
}

Check the implementation of DBExceptionas the root cause of the problem discussed.

检查执行DBException作为讨论问题的根本原因。