如何在Java中正确定义和处理参数化错误消息?

4

示例

我看过一些具有以下类似约定的.properties文件:

示例 1:

error.connection="Could not connect to host {0}, with IP address: {1}"

例子2:

error.connection="Could not connect to host %s, with IP address: %s."

(以及以下类似的XML文件)示例3:
<Error id="connection">Could not connect to host {0}, with IP address {1}</Error>

像示例2中定义的错误消息可以按如下方式处理:

// Get error.connection value from .properties file and store it in a variable named ERROR_MESSAGE.
String errorMessage = String.format(ERROR_MESSAGE, hostname, ipAddress);

这种方法似乎很笨拙和危险,因为它要求错误消息的用户在调用String.format时知道消息字符串需要多少个变量。
问题:
定义和处理变量错误消息的最佳实践是什么(包括可用的工具)? 这种做法叫什么?
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接