我正在尝试将一些数据插入名为rmas
的表中。
表格格式为
|rcode|rname|vcode|vanme
在这里,我为rcode设置了主键。
当我使用现有的rcode插入记录时,它显示类似于ORA-0000-1唯一约束已违反的记录。
如果我使用以下代码,即使出现其他错误,它也会显示该消息。
catch(Exception e)
{
//out.println("rcode already exists");
}
因此,我只想捕获该主键异常并显示为“ rcode已经存在”。可能吗?如果是,那怎么办?
提前致谢
最佳答案
Exception
是所有异常的父级。如果您编写了catch(Exception e) { }
块,则所有异常都将属于此类别。您需要找到编译器返回的异常。假设如果您的编译器返回此异常SQLIntegrityConstraintViolationException
,则将出现以下块
catch(SQLIntegrityConstraintViolationException e)
{
// Error message for integrity constraint violation
}
catch(NullPointerException e)
{
// Null Pointer exception occured.
}
catch(Exception e)
{
// Other error messages
}
这样,您可以拥有任意数量的异常块。但是请确保先编写更具体的异常类型,然后再编写父异常