捕获错误的异常

Pat*_*eck 3 java mysql error-handling exception-handling exception

我试图用MySQLin 来捕获一个特定的异常Java.但是,它正在运行catch (SQLException ex)而不是我想要的那个.

catch (MySQLIntegrityConstraintViolationException ex) {
}
catch (SQLException ex) {
}
Run Code Online (Sandbox Code Playgroud)

得到以下错误,我希望它运行该catch (MySQLIntegrityConstraintViolationException ex)功能.

11:12:06 AM DAO.UserDAO createUser
SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'idjaisjddiaij123ij' for key 'udid'
Run Code Online (Sandbox Code Playgroud)

它为什么运行catch (SQLException ex)而不是catch (MySQLIntegrityConstraintViolationException ex)

luk*_*yer 6

确保使用正确的命名空间.对我来说,附在图像上的一个像魅力一样.

正确的命名空间