在catch块中遇到return语句

时间:2014-04-21 10:02:47

标签: java android return try-catch

我正在开发一个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;
    } 

我犯了什么错误?

1 个答案:

答案 0 :(得分:0)

如果方法不知道如何处理异常,为什么要抓住它呢?我只是抛出它,让调用方法处理事情。那么也许你甚至可能会失去一个返回类型。

另外,作为一个无关的旁注:方法应该只做一件事。它们应该很小。你的确非常庞大,最应该被分解成更小的部分。

编辑:关于为什么没有抓住您的例外情况。您构建了一个嵌套的try块,可以静默处理任何异常。你的外部尝试块永远不会有任何线索。这是另一个例子,说明为什么捕获泛型异常是一件坏事。