当只允许SQLException时,如何抛出EOFException?

时间:2012-12-13 16:22:52

标签: java exception eofexception

java.sql.Statement.execute是否可能抛出java.io.EOFException?

http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#execute%28java.lang.String,%20int%29中,据说它只能抛出java.sql.SQLException。但是,我记录了这条异常消息:

** BEGIN NESTED EXCEPTION **

java.io.EOFException

STACKTRACE:

java.io.EOFException
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1394)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:1538)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1929)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
        at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1224)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2244)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2192)
        at com.mysql.jdbc.Statement.execute(Statement.java:906)
        at com.mysql.jdbc.Statement.execute(Statement.java:947)
        at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.logicalcobwebs.proxool.ProxyStatement.invoke(ProxyStatement.java:100)
        at org.logicalcobwebs.proxool.ProxyStatement.intercept(ProxyStatement.java:57)
        at $java.sql.Statement$$EnhancerByCGLIB$$4e952ca0.execute()
        at handler.do_insert(handler.java:734)

在“handler.java”的第733行有“执行”调用,在734有一个调用getGeneratedKeys(也可能只抛出SQLException)。

2 个答案:

答案 0 :(得分:4)

查看“BEGIN NESTED EXCEPTION”让我觉得EOFException嵌套在SQLException中 - 可能是这种情况吗?

该方法只能抛出SQLException,但SQLException异常可以包装另一个异常(或错误)

答案 1 :(得分:2)

查找单词** BEGIN NESTED EXCEPTION **execute()不抛出此异常,它是嵌套异常。这意味着在创建SQLException时传递的是原因。像这样:

try {

    ...

} catch (EOFException e) {
    throw new SQLException(e);
}