【问题标题】:Java Unreachable code errorJava 无法访问的代码错误
【发布时间】:2013-02-08 02:38:42
【问题描述】:

我正在为 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) 
            {

            }
        }
    }

}

【问题讨论】:

  • if(!getApi().checkSecurePassword(session, dbPword, pass)); .....消除 ';'无法访问的代码是编译时间
  • 在这里我想编译器只是知道他永远不会有一个有效的密码。
  • (这是将{ 与条件语句放在同一行的另一个很好的论据——那时分号在视觉上会更加不合适。)

标签: java exception smartfoxserver


【解决方案1】:

您的第二个 if 语句以 ; 终止,这是一个有效的语句。并且您在下一个块中抛出异常,这就是错误的原因。

if(!getApi().checkSecurePassword(session, dbPword, pass));

上面的 if 语句以分号结束,这是一个有效的语句,并且 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); 永远不会到达。

【讨论】:

    【解决方案2】:

    在上一个代码块之前有一个伪造的;

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

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

    【讨论】:

      【解决方案3】:

      因为在第二个 if 之后有一个不需要的 ;,所以下面的 {} 块将总是被执行。这意味着 SFSLoginException总是被抛出,执行将跳转到 catch

      这将导致永远不会调用 setProperty 方法。

      您需要从代码中的以下语句中删除分号:

      if(!getApi().checkSecurePassword(session, dbPword, pass));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 2016-07-09
        相关资源
        最近更新 更多