[英]Java exception message length
I am designing a database table, which will log Java exception messages. 我正在设计一个数据库表,它将记录Java异常消息。
For this case (and also out of interest) I want to know the (max.) message sizes I will encounter. 对于这种情况(也有兴趣),我想知道我将遇到的(最大)消息大小。
At the moment I am only interested in standard java language exceptions. 目前我只对标准的java语言异常感兴趣。
Example: 例:
try {
//Some code, which throws an Exception
} catch(Exception ex) {
String msg = ex.toString(); //Use toString, because sometimes there is no message
int size = msg.length(); //How large can this be?
}
Throwable toString() implementation: Throwable toString()实现:
public String toString() {
String var1 = this.getClass().getName();
String var2 = this.getLocalizedMessage();
return var2 != null ? var1 + ": " + var2 : var1;
}
Thanks for the replies. 谢谢你的回复。 Bonus question: Is there a list with all the java language exception messages (in English), so I do not have to check the source files manually?
奖金问题:是否有包含所有java语言异常消息的列表(英文),所以我不必手动检查源文件?
The Exception.getMessage()
returns a String
which doesn't have a length limit. Exception.getMessage()
返回一个没有长度限制的String
。 There is no such thing as "standard" message length, it will depend on whatever libraries you are using in your codebase eg validation exceptions from javax.validation
could be very long while NullPointerException
often has no message. 没有“标准”消息长度,它将取决于您在代码库中使用的任何库,例如来自
javax.validation
验证异常可能非常长,而NullPointerException
通常没有消息。
You should get few days of logs from your application and write a regex to measure the expected length of an exception. 您应该从应用程序中获取几天的日志,并编写一个正则表达式来衡量异常的预期长度。 Having this value you can make an educated guess.
拥有这个价值你可以做出有根据的猜测。
Either define a VARCHAR
column with given length and truncate the exception message when it's too long or define a blob/clob column and store the whole String
. 定义具有给定长度的
VARCHAR
列,并在异常消息太长时截断该异常消息,或者定义blob / clob列并存储整个String
。 The deciding factor could be your database performance eg Oracle allows VARCHAR
to have only 4000 bytes . 决定因素可能是您的数据库性能,例如Oracle允许
VARCHAR
只有4000个字节 。
As far as I know there is no limitation on message length, nut in most cases The message is short in order to be practical. 据我所知,对消息长度没有限制,大多数情况下坚果消息很短,以便切实可行。 You probably will be safe with max size 2000 chars, but make it in DB as varchar so it takes only the space equal to the size of actual message in each case.
使用max size 2000 chars可能会很安全,但是在DB中将它作为varchar使用,因此在每种情况下它只需要等于实际消息大小的空间。 Also exception's method
toString()
wouldn't give you any meaningful info. 异常的方法
toString()
也不会给你任何有意义的信息。 If there is no message then just store it as null or empty string "". 如果没有消息,则只将其存储为null或空字符串“”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.