【问题标题】:Stuck with return statement within catch block [duplicate]卡在 catch 块中的 return 语句[重复]
【发布时间】:2014-06-05 09:10:33
【问题描述】:

我正在开发一个使用以下 sn-p 的 android 应用程序:

  private Boolean updateAffectedProducts(boolean isOnline) {
        try {
            if (isOnline) {
                METHOD_NAME = "_updateAffectedDesignchangeInfo";

                String SOAP_ACTION = NAMESPACE + METHOD_NAME;

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                request.addProperty("InventoryId",
                        updateAffectedProduct_Model.getInventory_Id());
                request.addProperty("AttributeId",
                        updateAffectedProduct_Model.getAttributeId());
                request.addProperty("AttributeValue",
                        updateAffectedProduct_Model.getAttributeName());
                request.addProperty("OldValue",
                        updateAffectedProduct_Model.getCurrentValue());
                request.addProperty("NewValue",
                        updateAffectedProduct_Model.getNewValue());
                request.addProperty("Comments",
                        updateAffectedProduct_Model.getComments());
                request.addProperty("ClientId",
                        updateAffectedProduct_Model.getClientId());
                request.addProperty("ProjectId",
                        updateAffectedProduct_Model.getProjectId());
                request.addProperty("ProductId",
                        updateAffectedProduct_Model.getProductId());
                request.addProperty("WorkflowTransId",
                        updateAffectedProduct_Model.getWorkflowTransId());
                request.addProperty("BulletinId",
                        updateAffectedProduct_Model.getBulletinId());
                request.addProperty("CreatedBy",
                        updateAffectedProduct_Model.getCreatedBy());

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.dotNet = true;

                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.call(SOAP_ACTION, envelope);

                if (envelope.getResponse().toString().equals("true")) {
                    return getDesignChangeInfo(isOnline);
                } else {
                    return false;
                }
            } 

            else {

                String insertQuery="Insert into ProductAttributeChangeLog (ProductAttributeChangeId,AttributeId,ProjectID,ProductID, ClientID, OldValue, NewValue,InventoryID, LogComments, WorkFlowTransId, BulletinID, CreatedBy, Createdtime, IsDeleted) values " +
                        "('"+"1"+"','"+updateAffectedProduct_Model.getAttributeId()+"', '"+updateAffectedProduct_Model.getProjectId()+"', '"
                        +updateAffectedProduct_Model.getProductId()+"','"
                        +updateAffectedProduct_Model.getClientId()+"', '"
                        +updateAffectedProduct_Model.getCurrentValue()+"','"
                        +updateAffectedProduct_Model.getNewValue()+"', '"
                        +updateAffectedProduct_Model.getInventory_Id()+"', '"+""+"','"
                        +updateAffectedProduct_Model.getComments()+"','"
                        +updateAffectedProduct_Model.getWorkflowTransId()+"','"
                        +updateAffectedProduct_Model.getBulletinId()+"','"
                        +updateAffectedProduct_Model.getCreatedBy()+"','"
                        +sdf.format(now)+"','"+""+"')";

                //database.getWritableDatabase().execSQL(insertQuery);

                try
                {
                ContentValues insertProductAttributeChangeLog=new ContentValues();
                insertProductAttributeChangeLog.put("ProductAttributeChangeId", 1);
                insertProductAttributeChangeLog.put("AttributeId", updateAffectedProduct_Model.getAttributeId());
                insertProductAttributeChangeLog.put("ProjectID", updateAffectedProduct_Model.getProjectId());
                insertProductAttributeChangeLog.put("ProductID", updateAffectedProduct_Model.getProductId());
                insertProductAttributeChangeLog.put("ClientID", updateAffectedProduct_Model.getClientId());
                insertProductAttributeChangeLog.put("OldValue", updateAffectedProduct_Model.getCurrentValue());
                insertProductAttributeChangeLog.put("NewValue", updateAffectedProduct_Model.getNewValue());
                insertProductAttributeChangeLog.put("InventoryID", updateAffectedProduct_Model.getInventory_Id());
                insertProductAttributeChangeLog.put("LogComments", updateAffectedProduct_Model.getComments());
                insertProductAttributeChangeLog.put("Comment", updateAffectedProduct_Model.getComments());
                insertProductAttributeChangeLog.put("WorkFlowTransId", updateAffectedProduct_Model.getWorkflowTransId());
                insertProductAttributeChangeLog.put("BulletinId", updateAffectedProduct_Model.getBulletinId());
                insertProductAttributeChangeLog.put("CreatedBy", updateAffectedProduct_Model.getCreatedBy());
                insertProductAttributeChangeLog.put("UpdatedBy", updateAffectedProduct_Model.getCreatedBy());
                insertProductAttributeChangeLog.put("Createdtime", sdf.format(now));
                insertProductAttributeChangeLog.put("UpdatedTime", sdf.format(now));
                insertProductAttributeChangeLog.put("IsDeleted", "false");


                database.getWritableDatabase().insert("ProductAttributeChangeLog", null, insertProductAttributeChangeLog);

                database.getWritableDatabase().close();
                }
                catch(Exception e)
                {
                e.printStackTrace();    
                }

                //Cursor c=database.getReadableDatabase().rawQuery("SELECT * FROM ProductAttributeChangeLog where ", selectionArgs)

    /*          //database.getWritableDatabase().execSQL("INSERT INTO Queries (Queries,QueryType) values('"+insertQuery+"',"+"'0')");
                ContentValues insertQueryContent=new ContentValues();
                insertQueryContent.put("Queries", insertQuery);
                insertQueryContent.put("QueryType", "0");
                database.getWritableDatabase().insert("Queries", null, insertQueryContent);*/

                 getDesignChangeInfo(isOnline);
                return true;

            }
        } 
        catch (SocketTimeoutException e) {
            this.e = e;
            return false;
        } catch (IOException e) {
            this.e = e;
            return false;
        } catch (XmlPullParserException e) {
            this.e = e;
            return false;
        } catch (Exception e) { 
            this.e = e;

        return false;
        }


    } 

在我调试代码时。最后一个catch块return false;中的return语句在不执行this.e = e;的情况下执行,this.enull。如果我删除最后一个 catch 块

 catch (Exception e) { 
        this.e = e;
        return false;
    }

然后执行后面的catch块中的return语句

  catch (XmlPullParserException e) {
        this.e = e;
        return false;
    } 

我犯了什么错误?

【问题讨论】:

  • 它是重复的。这是同一个人的同一个问题……为什么要问两次?
  • 另外,很难相信this.e is null 是真的

标签: java android return try-catch


【解决方案1】:

如果方法不知道如何处理异常,为什么要捕获它呢?我只是把它扔掉,让调用方法来处理事情。那么也许你甚至可以使用 void 返回类型。

另外,作为一个不相关的旁注:方法应该只做一件事。它们应该很小。你的真的很大,应该很确定地分解成更小的部分。

编辑:关于为什么你的异常没有被捕获。您已经构建了一个嵌套的 try 块,它默默地处理任何异常。您的外部尝试块永远没有线索。这是另一个例子说明为什么捕获泛型异常是一件坏事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-21
    • 2014-04-19
    • 2016-03-07
    • 2013-10-02
    • 2020-06-17
    • 1970-01-01
    • 2014-10-10
    • 2011-08-07
    相关资源
    最近更新 更多