String.format 使用 exception.getMessage() 作为格式
我有一个与 JAVA 中的 String.format 相关的问题。 我的 HibernateDao 类负责持久化实体,并在发生任何约束违规时抛出异常。该消息包含 %s 并将用作上层的格式,因为我应该担心这一层中的类型,因此无法识别我无法持久化的对象。
public Entity persistEntity(Entity entity) {
if (entity == null || StringUtils.isBlank(entity.getId()))
throw new InternalError(CANNOT_INSERT_NULL_ENTITY);
try {
getHibernateTemplate().save(entity);
} catch (DataAccessException e) {
if (e.getCause() instanceof ConstraintViolationException)
throw new HibernateDaoException("%s could not be persisted. Constraint violation.");
throw new HibernateDaoException(e);
}
return entity;
}
然后在我的 DaoHelper 类中,我将捕获此异常并抛出一个新异常,并带有格式化消息。
//Correct Code
public Entity create(Entity object) throws MyException {
try {
return this.hibernateDao.persistEntity(object);
} catch (HibernateDaoException he) {
String format = he.getMessage();
throw new MyException(String.format(format,object.getClass().getSimpleName()));
}
}
我的问题是,为什么我不能在 String.format 方法中直接调用 he.getMessage() ?并且必须使用 'tmp' 变量来代替...它只是不会替换字符串中的 %s 。
//What I wished to do, but I cant.
public Entity create(Entity object) throws MyException {
try {
return this.hibernateDao.persistEntity(object);
} catch (HibernateDaoException he) {
throw new MyException(String.format(he.getMessage(),object.getClass().getSimpleName()));
}
}
提前谢谢。
I have a question related to String.format in JAVA.
My HibernateDao Class is responsible for persisting entities and will throw an exception in case I have any constrain violation. The message contains a %s and will be used as a format in the upper layers, as I should be worried about types in this layer, thus can't identify what object I could not persist.
public Entity persistEntity(Entity entity) {
if (entity == null || StringUtils.isBlank(entity.getId()))
throw new InternalError(CANNOT_INSERT_NULL_ENTITY);
try {
getHibernateTemplate().save(entity);
} catch (DataAccessException e) {
if (e.getCause() instanceof ConstraintViolationException)
throw new HibernateDaoException("%s could not be persisted. Constraint violation.");
throw new HibernateDaoException(e);
}
return entity;
}
And then in my DaoHelper Class I will catch this exception and throw a new one, with a formatted message.
//Correct Code
public Entity create(Entity object) throws MyException {
try {
return this.hibernateDao.persistEntity(object);
} catch (HibernateDaoException he) {
String format = he.getMessage();
throw new MyException(String.format(format,object.getClass().getSimpleName()));
}
}
My question is, why I cannot directly call the he.getMessage() in my String.format method?? And must use a 'tmp' variable instead... It just won't substitute the %s from the string.
//What I wished to do, but I cant.
public Entity create(Entity object) throws MyException {
try {
return this.hibernateDao.persistEntity(object);
} catch (HibernateDaoException he) {
throw new MyException(String.format(he.getMessage(),object.getClass().getSimpleName()));
}
}
Thx in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该关闭,因为预期的行为正在发挥作用。正如 @Kal 和 @highlycaffelated 评论的那样,直接调用 getMessage() 确实有效,我的构建肯定发生了一些事情并且没有正确更新。不过,这些消息现在确实显示正确。
感谢您的快速解答:)
This should be closed, as the intended behavior is working. As @Kal and @highlycaffeinated commented, calling the
getMessage()
directly does work, something must have happened with my build and did not update correctly. However the messages do appear correctly now.Thanks for the quick answers :)