Java不可达代码错误

3

我正在为SmartFox服务器扩展创建一个Java类。它试图访问MySQL数据库。

我在session.setProperty("DatabaseID", dbId);这一行收到一个名为Unreachable Code的错误。

package sfs2x.extension.test.dblogin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class LoginEventHandler extends BaseServerEventHandler 
{
    @Override
    public void handleServerEvent(ISFSEvent e) throws SFSException 
    {
        String email = (String)e.getParameter(SFSEventParam.LOGIN_NAME);
        String pass = (String)e.getParameter(SFSEventParam.LOGIN_PASSWORD);
        ISession session = (ISession)e.getParameter(SFSEventParam.SESSION);

        IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
        Connection connection = null;

        try
        {
            connection = dbManager.getConnection();

            PreparedStatement stmt = connection.prepareStatement("SELECT * FROM users WHERE email=?");
            stmt.setString(1, email);

            ResultSet res = stmt.executeQuery();

            if(!res.first())
            {
                SFSErrorData errData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
                errData.addParameter(email);

                throw new SFSLoginException("Bad user name: "+ email, errData);
            }

            String dbPword = res.getString("password");
            int dbId = res.getInt("id");

            if(!getApi().checkSecurePassword(session, dbPword, pass));
            {
                SFSErrorData errorData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
                errorData.addParameter(email);

                throw new SFSLoginException("Bad password for user: "+ email, errorData);
            }

            session.setProperty("DatabaseID", dbId);
           //UNREACHABLE CODE
           //IF I COMMENT THIS OUT, THERE IS NO UNREACHABLE CODE ERROR

        }

        catch(SQLException eve)
        {
            SFSErrorData erroData = new SFSErrorData(SFSErrorCode.GENERIC_ERROR);
            erroData.addParameter("SQL Error: " + eve.getMessage());

            throw new SFSLoginException("A SQL Error occurred: " + eve.getMessage(), erroData);
        }

        finally
        {
            try 
            {
                connection.close();
            }
            catch (SQLException e1) 
            {

            }
        }
    }

}

如果(!getApi().checkSecurePassword(session, dbPword, pass)) { .....删除 ';' 不可到达的代码在编译时。} - Its not blank
我当时还在想编译器只是知道他永远不会有一个有效的密码。 - Hot Licks
2
这又是一个支持将“{”放在条件语句同一行的好理由——分号将会显得更加不协调。 - Hot Licks
3个回答

7
您的第二个if语句以; 终止,这是一个有效的语句。而在下一个代码块中,您抛出了异常,这就是错误的原因。
if(!getApi().checkSecurePassword(session, dbPword, pass));

以上的if语句以分号终止,这是一个有效的语句,如果语句将对其进行操作。您代码的其他部分在if语句无论如何都会执行,这最终导致异常。

{
    SFSErrorData errorData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
    errorData.addParameter(email);

    throw new SFSLoginException("Bad password for user: "+ email, errorData);
}

这就是你遇到错误的原因,因为你的代码 session.setProperty("DatabaseID", dbId); 永远不会执行。

3

在之前的代码块前面有一个错误的;:

if(!getApi().checkSecurePassword(session, dbPword, pass));
                                                      // ^
                                                      // |
                                                      // +---- remove this ';'
{
   ...
   throw new SFSLoginException("Bad password for user: "+ email, errorData);
}

session.setProperty("DatabaseID", dbId);

因此,throw 总是被执行,因此代码永远不会到达 session.setProperty()

0
由于第二个if后面有一个不需要的分号";",接下来的"{}"块将总是被执行。这意味着将总是抛出一个异常并且执行会跳转到块。
这将导致方法永远不会被调用。
您需要从代码中的以下语句中删除分号: if(!getApi().checkSecurePassword(session, dbPword, pass));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接