---
title: "thrift tutorial --version 0.8 by java上InvalidOperation异常未被client抛出"
author: "zb_86"
date: "2011-12-24T16:24:10Z"
tags: []
categories: ["开源软件"]
slug: "thrift-tutorial-version-0-8-by-java-invalidoperation-client"
draft: false
lastmod: "2011-12-24T16:25:05Z"
id: 1324686
url: "https://zbszone.iteye.com/blog/1324686"
---

走thrift tutorial得例子：

发现一个问题，在client发送 （1 DIVIDE 0）:{  
     work.op = Operation.DIVIDE;  
     work.num1 = 1;  
     work.num2 = 0;  
     } 这种请求得时候，server本来应该返回InvalidOperation异常，client捕获。但是Server确实返回了，但是client并没有得到异常，而是success。

跟踪代码，发现server确实write 了异常到struct中，但是client read得时候，也read成功，不过在Calculator.java得2523行得“struct.setSuccessIsSet(true);”这行代码执行后，read返回得结果会根据isSuccessIsSet来返回结果，所以因此虽然server返回了异常InvalidOperation，但是因为isSuccessIsSet=true，所以client得“int quotient = client.calculate(1, work);” 不会抛出异常，直接返回success得默认值0.

见代码：  
     public void read(org.apache.thrift.protocol.TProtocol iprot, calculate\_result struct) throws org.apache.thrift.TException {  
        System.out.println("calculate\_resultStandardScheme read start!!!!!!!!");  
        org.apache.thrift.protocol.TField schemeField;  
        iprot.readStructBegin();  
        while (true)  
        {  
          schemeField = iprot.readFieldBegin();  
          if (schemeField.type == org.apache.thrift.protocol.TType.STOP) {  
            break;  
          }  
          switch (schemeField.id) {  
            case 0: // SUCCESS  
              System.out.println("calculate\_resultStandardScheme read SUCCESS!!!!!!!!");  
              if (schemeField.type == org.apache.thrift.protocol.TType.I32) {  
                struct.success = iprot.readI32();  
                // struct.setSuccessIsSet(true);   // 这里默认是没有注释得。我注释了，异常就正常抛出了。  
              } else {  
                org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);  
              }  
              break;  
            case 1: // OUCH  
              System.out.println("calculate\_resultStandardScheme read OUCH!!!!!!!!");  
              if (schemeField.type == org.apache.thrift.protocol.TType.STRUCT) {  
                struct.ouch = new InvalidOperation();  
                struct.ouch.read(iprot);  
                struct.setOuchIsSet(true);  
                System.out.println("calculate\_resultStandardScheme read OUCH111111");  
              } else {  
                org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);  
              }  
              break;  
            default:  
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);  
          }  
          iprot.readFieldEnd();  
        }  
        iprot.readStructEnd();  
          
          System.out.println("calculate\_resultStandardScheme struct:" + struct);  
        // check for required fields of primitive type, which can't be checked in the validate method  
        struct.validate();  
      }