管理 Java 中的异常

发布于 2024-11-17 22:50:19 字数 656 浏览 3 评论 0原文

我正在做一个小型数据库程序。

Connection connection = DriverManager.getConnection(..., ..., ...);

String createTable= "CREATE TABLE Employee ...";

try
{
    Statement stmt = connection.createStatement ();
    stmt.executeUpdate(createTable);
    stmt.close();
    connection.close ();
}
catch(Exception Ex)
{
    System.out.println("Erreur : " + Ex.toString());
}  

当您运行该程序时,一切都会好起来,但第二次您会收到此异常:

重复的表名称:Employee

好的,我知道如何管理异常,但是如何管理每个可能的异常。就像:

IF 异常是重复错误THEN 显示自定义重复消息。

IF 这是一个重复的主键THEN 显示另一条错误消息,依此类推。

谢谢。

I'm doing a small database program.

Connection connection = DriverManager.getConnection(..., ..., ...);

String createTable= "CREATE TABLE Employee ...";

try
{
    Statement stmt = connection.createStatement ();
    stmt.executeUpdate(createTable);
    stmt.close();
    connection.close ();
}
catch(Exception Ex)
{
    System.out.println("Erreur : " + Ex.toString());
}  

When you run the program everything is going to be fine, but the second time you will get this Exception :

Duplicate table name : Employee

Ok fine I know how to manage Exceptions, but how about managing every possible Exception. Like :

IF the Exception is a Duplicattion error THEN display a custom Duplicate message.

IF it's a duplicate primary key THEN display another error message and so on.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弥繁 2024-11-24 22:50:19

如果要处理特定异常,则需要多个 catch 语句。

try {
    // some code that may throw an exception
}
catch (DuplicateKeyException e) {
    // handle a duplicate key exception
}
catch (DuplicateTableException e) {
    // handle a duplicate table exception - note this probably isn't the correct exception, you'll need to look up what is actually thrown
}
catch (Exception e) {
    // handle all other exceptions in a non-specific way
}

You need multiple catch statements if you want to handle specific exceptions.

try {
    // some code that may throw an exception
}
catch (DuplicateKeyException e) {
    // handle a duplicate key exception
}
catch (DuplicateTableException e) {
    // handle a duplicate table exception - note this probably isn't the correct exception, you'll need to look up what is actually thrown
}
catch (Exception e) {
    // handle all other exceptions in a non-specific way
}
ゃ懵逼小萝莉 2024-11-24 22:50:19

您必须解析异常方法字符串才能获取实际 SQL 异常的“子类型”:

 try {
   // ...
 } catch (SQLException e) {
  if (e.getMessage().toLowerString().contains("duplicate table name")) {
   // handle duplicate table name problem
  } else if ( /* ... */ ) {
   // ...
  }
 }

You'll have to parse the exceptions method string to get the "subtype" of the actual SQL exception:

 try {
   // ...
 } catch (SQLException e) {
  if (e.getMessage().toLowerString().contains("duplicate table name")) {
   // handle duplicate table name problem
  } else if ( /* ... */ ) {
   // ...
  }
 }
太傻旳人生 2024-11-24 22:50:19

如果您使用 Spring JDBC 库,它会为您做一些异常转换,因此您将得到(未经检查的)异常,例如 DuplicateKeyExceptionTypeMismatchDataAccessException 等,您可以将这些异常转换为 Spring JDBC 库。可以单独抓。

If you use the Spring JDBC library, it will do some exception translation for you, so you'll get (unchecked) exceptions like DuplicateKeyException, TypeMismatchDataAccessException, etc., which you can catch separately.

≈。彩虹 2024-11-24 22:50:19

您还可以通过根据 SQLException 的错误代码定义特定行为来为 sql-Exception 排序正确的错误处理。但请注意:错误代码是特定于实现的,这意味着例如 JavaDB 的一个错误代码如果来自另一个 dbms,则不具有相同的含义。


JAVA API 中的 SQLException.getErrorCode()

You could also sort the right error handling for you sql-exception by defining a specific behavior depending on the error-code of the SQLException. But watch out: the error codes are implementation specific, that means one error code of for example JavaDB does not have the same meaning if it comes from another dbms.

see
SQLException.getErrorCode() in the JAVA API

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文