尝试catch内部抛出IOException

时间:2016-02-23 04:17:02

标签: java android

我有这样的功能

function pipeFix(numbers) {
  var firstNum = numbers[0];
  var lastNum = numbers[numbers.length - 1];
  var iterations = (lastNum - firstNum) + 1;
  var arr = [];
  for (var i = firstNum; i <= iterations; i++) {
    arr.push(i);
  }
  return arr;
}

如果函数 public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); try{ Utilities.showRequestLog(request,response,TAG); }catch(IOException e){ Log.e(TAG,e.getMessage()); return response; } return response; } 具有intercept,则运行Utilities.showRequestLog(request,response,TAG);方法。它永远不会去捕获阻止并始终抛出Exception。我的问题是,如果Exception有异常,如何让我的代码转到 catch

编辑:

这是我的Utilities.showRequestLog(request,response,TAG);。因为我只想显示请求和响应日志(对于调试案例)。因此,如果Utilities.showRequestLog(request,response,TAG);函数获得异常,showRequestLog函数仍会返回显示数据的响应。当showRequestLog有异常时,我不会抛出异常,因为它真的是另一种感觉

intercept

3 个答案:

答案 0 :(得分:0)

[apikey] => xxx [file] => @/absolute/path/to/file.exe;type=application/octet-stream;filename=file.exe 只有在抛出任何其他运行时异常时才会抛出Utilities.showRequestLog。您可以添加另一个catch块 IOException,理想情况下,如果我们不知道如何处理它们,我们就不应该捕获运行时异常。

答案 1 :(得分:0)

测试此代码。

var obj = JSON.parse(text);

        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);
            try{
               Utilities.showRequestLog(request,response,TAG);
            }catch(IOException e){
               Log.e(TAG,e.getMessage());
               throw new IOException(e.getMessage())
            }
            return response;
        }

答案 2 :(得分:0)

试试这个。

try{
    Utilities.showRequestLog(request,response,TAG);
} catch(IOException e) {
    Log.e(TAG,e.getMessage());
    return response;
} catch (Exception ex) {
    // do something for Exception other than IOException
}
相关问题