आप सामान्य JDBCException
पकड़ कर सकते हैं या तो:
try {
userDao.save(user); //might throw exception
} catch(JDBCException e) {
//Error during hibernate query
}
या आप भी JDBCException
ऐसे ConstraintViolationException
के रूप में या JDBCConnectionException
के अधिक विशिष्ट उपवर्गों में से एक को पकड़ कर सकते हैं:
try {
userDao.save(user); //might throw exception
} catch(ConstraintViolationException e) {
//Email Address already exists
} catch(JDBCConnectionException e) {
//Lost the connection
}
और साथ e.getCause()
विधि आप अंतर्निहित SQLException
पुनर्प्राप्त कर सकते हैं और इसे आगे विश्लेषण कर सकते हैं:
try {
userDao.save(user); //might throw exception
} catch(JDBCException e) {
SQLException cause = (SQLException) e.getCause();
//evaluate cause and find out what was the problem
System.out.println(cause.getMessage());
}
कौन सा उदाहरण के लिए मुद्रित होगा: Duplicate entry 'UserTestUsername' for key 'username'