Note that there are some explanatory texts on larger screens.

plurals
  1. POJava executes 'catch' block with no error, and somehow still runs 'try' block
    primarykey
    data
    text
    <p>I have a problem with the following code. First, I am a newbie to Java with experience with several other languages.</p> <p>When I run the following on Android, it errors at "in.close();", jumps to the catch block and runs Return ""; The thing is, the function doesn't return an empty string, it successfully returns the correct json data even though the debugger says it isn't running return json, but that it's running return "";</p> <pre><code>public void fetch() { // Get the JSON of the image gallery String json = getJSON(); // json somehow has the actual correct json data and not an empty string! }; private String getJSON() { try { // Create a URL for the desired page URL url = new URL("http://imgur.com/gallery.json"); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); String json = in.readLine(); in.close(); return json; // &lt;-- debugger doesn't break here! } catch(Exception e) { Log.e("getJSON", e.getMessage()); return ""; // &lt;-- debugger breaks here! Cat Log shows nothing! } } </code></pre> <p>I copy the exact same code into a Java console so that I can output the error message and watch it in the debugger with break points, and the code executes without error.</p> <pre><code> try { // Create a URL for the desired page URL url = new URL("http://imgur.com/gallery.json"); // Read all the text returned by the server BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String json = in.readLine(); in.close(); System.out.println(json); } catch(Exception e) { System.out.println(e.getMessage()); } </code></pre> <p>What exactly is going on? Why is my code run on Android erroring when it runs in a Java Console just fine? Why when it errors does it not return what the error is supposed to return; and empty string? How can I view the error message in Android?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload