String.format使用exception.getMessage()作为格式 [英] String.format using a exception.getMessage() as a format

查看:211
本文介绍了String.format使用exception.getMessage()作为格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JAVA中有一个与String.format有关的问题。
我的HibernateDao类负责持久化实体,并将抛出异常,以防我有任何约束违规。该消息包含一个%s,并将被用作上层的格式,因为我应该担心这个层中的类型,因此无法识别出我无法保留的对象。

  public Entity persistEntity(Entity entity){
if(entity == null || StringUtils.isBlank(entity.getId()))
抛出新的InternalError(CANNOT_INSERT_NULL_ENTITY);

try {
getHibernateTemplate()。save(entity);
} catch(DataAccessException e){
if(e.getCause()instanceof ConstraintViolationException)
抛出新的HibernateDaoException(%s无法持久化,约束违规);
抛出新的HibernateDaoException(e);
}

返回实体;
}

然后在我的DaoHelper类中,我将捕获此异常并抛出一个新的使用格式化的消息。

  //正确的代码
public Entity create(Entity object)throws MyException {
try {
return this.hibernateDao.persistEntity(object);
} catch(HibernateDaoException他){
String format = he.getMessage();
throw new MyException(String.format(format,object.getClass()。getSimpleName()));
}
}

我的问题是,为什么我不能直接打电话给他我的String.format方法中的.getMessage()并且必须使用'tmp'变量,而不是替换字符串中的%s。

  /我想做什么,但我不能。 
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。

解决方案

应该关闭,因为预期的行为是正常的。由于@Kal和@highlycaffeinated评论,直接调用 getMessage()可以正常工作,我的构建必须发生一些事情,并且没有正确更新。但是,消息现在显示正确。
感谢您的快速回答:)


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.

解决方案

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 :)

这篇关于String.format使用exception.getMessage()作为格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆