Created
October 19, 2011 12:24
-
-
Save galak-fyyar/1298147 to your computer and use it in GitHub Desktop.
Working with exceptions in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String getUnreadMessages() { | |
List<Message> messages = service.getMessage().getUnreadMessage(); | |
String result = convert( messages ); | |
markMessagesUpdated( messages ); | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String getUnreadMessages() { | |
boolean readSuccessful = true; | |
List<Message> messages = null; | |
try { | |
messages = service.getMessage().getUnreadMessage(); | |
return convert( messages ); | |
} catch ( Throwable t ) { | |
readSuccessful = false; | |
logger.fatal("Exception during processing messages", t); | |
} finally { | |
if ( readSuccessful ) { | |
markMessagesUpdated( messages ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ResultClass getOptimalSale( long id ) { | |
Map<Long, ResultClass> intermediateResult = new HashMap<Long, ResultClass>(); | |
try { | |
Query query = getCurrentSession().createQuery( "from SomeEntityClass e where e.parentId=:parentId" ). | |
setParameter( "parentId", id ); | |
//... really long algorithm | |
} catch ( Exception e ) { | |
logger.error( e.getMessage(), e ); | |
throw new SalesCalculationException( e ); | |
} | |
return intermediateResult.get( id ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ResultClass getOptimalSale( long id ) { | |
Map<Long, ResultClass> intermediateResult = new HashMap<Long, ResultClass>(); | |
try { | |
Query query = getCurrentSession().createQuery( "from SomeEntityClass e where e.parentId=:parentId" ). | |
setParameter( "parentId", id ); | |
//... really long algorithm | |
} catch ( Exception e ) { | |
logger.error( e.getMessage(), e ); | |
throw new RuntimeException( e ); | |
} | |
return intermediateResult.get( id ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
Session session = getCurrentSession(); | |
return session.createQuery( "from SomeEntityClass" ).list(); | |
} catch ( HibernateException e ) { | |
logger.error( getClass().getName() + " findAll()", e ); | |
throw e; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Session session = null; | |
ArrayList<SomeEntityClass> result = null; | |
try { | |
session = getCurrentSession(); | |
result = session.createQuery( "from SomeEntityClass" ).list(); | |
} catch ( HibernateException e ) { | |
logger.error( getClass().getName() + " findAll()", e ); | |
} finally { | |
session.close(); | |
} | |
return result; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
session = getCurrentSession(); | |
for ( SomeEntityClass entity : entities ) { | |
session.saveOrUpdate( entity ); | |
} | |
session.flush(); | |
} catch ( NonUniqueObjectException e ) { | |
try { | |
session.merge( entities ); | |
} catch ( HibernateException ex ) { | |
logger.error( getClass().getName() + " merge non unique object ", e ); | |
throw e; | |
} | |
} catch ( HibernateException e ) { | |
logger.error( getClass().getName() + " save ", e ); | |
throw e; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
session = getCurrentSession(); | |
for ( SomeEntityClass entity : entities ) { | |
session.saveOrUpdate( entity ); | |
} | |
session.flush(); | |
} catch ( Exception e ) { | |
logger.error( getClass().getName() + " save ", e ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment