I have following code using hibernate to throw a custom exception on error and I also want to close the session in this case, since the exception won\'t be catched unless receiv
While closeSessionAndThrow always throws the remote exception, the compiler doesn't dive into the method to find that out, therefore the compiler only knows that it COULD throw a RemoteException, not that it WILL. I would maybe do something like this:
public RemoteException closeSessionAndThrow(final Session session, final Exception e, String msg)
{
SessionManager.logger.log(Level.SEVERE, msg, e);
this.closeSession(session);
return new RemoteException(msg);
}
public T get(final Session session, final String queryName) throws RemoteException
{
final Query query = // query using given session ...
try
{
return (T) query.uniqueResult();
}
catch (final HibernateException e)
{
throw this.closeSessionAndThrow(session, e, "Could not retrieve Data");
}
}