Java Catch Try让我在Android Studio调试中感到困惑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19306672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:54:48  来源:igfitidea点击:

Catch Try has me confused in Android Studio debugging

javaandroidandroid-studio

提问by callmeDarwin

I'm trying to debug this bit of code in Android Studio. It all seem pretty straight forward but the 'return sb1' in the try won't allow me to break on it (a red dot with an x) and says it's not executable.

我正在尝试在 Android Studio 中调试这段代码。这一切看起来都很简单,但是尝试中的“返回 sb1”不允许我打破它(带有 x 的红点)并说它不可执行。

    public static String postRecordOnServer(String url,
       ArrayList<NameValuePair> nameValuePairs) {

       String message = "";
       InputStream is = null;
       try {
            HttpParams httpParameters = new BasicHttpParams();
            int timeoutConnection = 3000;
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);
            int timeoutSocket = 5000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine());
            String sb1 = sb.toString().toUpperCase();
            reader.close();
            is.close();
            return sb1;
        } catch (Exception e) {
            message = e.getMessage();
        }
        return "EXCEPTION:" + message;
    }

And even though it always returns the 'EXCEPTION' calling function gets a NULL result value. The Catch is never fired.

即使它总是返回 'EXCEPTION' 调用函数也会得到一个 NULL 结果值。Catch 永远不会被解雇。

Any suggestions?

有什么建议?

回答by ssantos

A couple tips you may find useful.-

您可能会发现一些有用的提示。-

  • Try directly to debug step by step by adding a breakpoint at the start of the method, just to check what's really happening.

  • Throw your exceptions. Returning a message with the exception is not the way you're supposed to handle issues inside your function. You should add some logic inside your catch block, or just make postRecordOnServerthrows Exception, and let the calling function to handle it instead.

  • 直接尝试通过在方法开始处添加断点来逐步调试,只是为了检查真正发生了什么。

  • 抛出你的例外。返回带有异常的消息不是您处理函数内部问题的方式。您应该在 catch 块中添加一些逻辑,或者只是 make postRecordOnServerthrows Exception,然后让调用函数来处理它。

回答by chutcher

Try "File->Invalidate Caches" and restart.

尝试“文件->无效缓存”并重新启动。

I think I just ran into something similar and it worked for me.

我想我只是遇到了类似的事情,它对我有用。

I used some code I found on the internet and it was throwing an exception. Instead of letting the exception bubble up they were putting the details into a custom object and they were passing that around. Well, I didn't like that and I attempted to change it but I had a tremendous amount of difficulty.

我使用了一些我在互联网上找到的代码,它抛出了一个异常。他们没有让异常冒泡,而是将详细信息放入自定义对象中,然后将其传递出去。嗯,我不喜欢那样,我试图改变它,但我遇到了巨大的困难。

There were three classes involved; A, B, and C. My code made a call to A, which made a call to B, which made a call to C, and C was throwing the exception.

涉及三个类;A、B 和 C。我的代码调用了 A,A 调用了 B,B 调用了 C,而 C 抛出了异常。

The stack trace was making no sense when the exception was thrown after my changes so I decided to make a call to C directly. When the exception was thrown I was still seeing A & B in the trace even though I had bypassed them. Then I completely deleted A & B from my project but they were still showing up in the trace. WTF!!!

当我更改后抛出异常时,堆栈跟踪没有任何意义,因此我决定直接调用 C。当抛出异常时,即使我绕过了它们,我仍然在跟踪中看到 A 和 B。然后我从我的项目中完全删除了 A 和 B,但它们仍然出现在跟踪中。跆拳道!!!

After some time pulling my hair out I invalided the caches, restarted, and problem was resolved.

一段时间后,我使缓存无效,重新启动,问题解决了。