处理的SQL异常被标记为未处理

时间:2015-08-11 15:54:48

标签: java exception-handling try-catch

好的,这是我的代码片段:

     try 
    {

        res = gfSQL.doSQL("SELECT TIMESTAMP, MSGLEVEL, APPLICATION, STREAM, THREADID, " +
                          " THREADNAME, REQUESTID, MESSAGE, ID, PROCESSED" +
                           " FROM GFLOG where PROCESSED = 'N'" +
                           " ORDER BY TIMESTAMP, ID");
    } catch ( SQLException e) {
        e.printStackTrace();
        System.exit(128);
    }

    if (! res.first()) {  <<<---- FLAGGED STATEMENT
        // no data to process  
        System.err.println("No data found to process\n");
        return;
    }

现在java正在使用&#39; Unhandled异常类型SQLException标记指示语句(以及后面的每个人)。 gfSQL.doSQL抛出此异常并定义为:

public ResultSet doSQL(String sqlCommand)抛出SQLException     {

有趣的是 - 如果我再说一遍&#39; catch&#39;块如此:

try 
    {

        res = gfSQL.doSQL("SELECT TIMESTAMP, MSGLEVEL, APPLICATION, STREAM, THREADID, " +
                          " THREADNAME, REQUESTID, MESSAGE, ID, PROCESSED" +
                           " FROM GFLOG where PROCESSED = 'N'" +
                           " ORDER BY TIMESTAMP, ID");
    } catch ( SQLException e) {
        e.printStackTrace();
        System.exit(128);
    } catch ( SQLException e) {
        e.printStackTrace();
        System.exit(128);
    }
    if (! res.first()) {
        // no data to process

没有&#39;未处理&#39;错误被标记。 (但重复的口号被标记为“无法到达的捕获块 - 当然已经处理了SQLException。”

任何想法为什么Java在一个实例中看到了捕获而在另一个实例中看不到?或者我错过了什么?

2 个答案:

答案 0 :(得分:0)

好吧,<div class="hsc"> <div class="title">Make me blue!</div> <div class="jobs">Jobs</div> </div> <div class="cc"> <div class="title">Make me red!</div> <div class="jobs">More jobs</div> </div>可以抛出一个SQLException,而你正在抓住它。

但是gfSQL.doSQL()也可以抛出一个SQLException,而你却没有捕获它,因为这个指令不在try块中。这就是你得到编译器错误的原因。

答案 1 :(得分:0)

DELIMITER // CREATE TRIGGER log_details_in_reverse AFTER INSERT ON logs FOR EACH ROW BEGIN DECLARE reversedLogDetail varchar(100); DECLARE rowId int; <-- you don't have a primary key in your example, but I'm assuming you do have one. If not, you should look into adding it. -- Reverse the column log_detail and assign it to the declared variable SELECT reverse(log_detail) INTO reversedLogDetail; SELECT mysql_insert_id() INTO rowId; -- Update record into logs table UPDATE logs SET log_detail_reverse = reversedLogDetail WHERE log_id = rowId; END; // DELIMITER ; 应该在try块中,因为它可以抛出异常:

res.first()