java: how to declare final a variable that is initialized inside a try - catch block?

前端 未结 4 2027
迷失自我
迷失自我 2021-01-01 19:14

I have a variable that is not supposed to change its value after it\'s been initialized, so I want to define it as a final variable.

the problem is that the variable

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-01 19:48

    Can you try assigning it in both the catch and finally blocks? Like so:

    Connection connTemp = null;
    final Connection conn;
    try {
        connTemp = getConn(prefix);
    } catch (Exception e) {
        throw new DbHelperException("error opening connection", e);
    } finally {
        closeConnection(conn);
    }
    conn = connTemp;
    

提交回复
热议问题