public void onClick() throws SQLException
try {
// Do something
} catch(MySQLIntegrityConstraintViolationException e) {
//Can i convert this exception to SQL Exception
}
Can i convert MySQLIntegrityConstraintViolationException to SQLException which is thrown by the method?
Sure you can wrap and rethrow – if you think it adds more information or makes your ideas more general. I think in this case the exception you’re catching gives more info than the one you’re thinking about.
I wouldn’t choose a SQLException, though. It’s a checked exception. I think the tide has turned from checked to unchecked exceptions.
Spring wraps SQLExceptions into unchecked DataAccessException that extends RuntimeException. I’d suggest that you follow suit.
Here’s how you should do it:
Don’t just pass the message. Give the whole stack trace.