2

This is what i have right now, this method open's a connection with http url,

public static void setCameraList(String list) {
        URL calculator;
        try {   
    String url = "http://example.com/index.php?cameraList=" +URLEncoder.encode(list, "ISO-8859-1");
            calculator = new URL(url);
            URLConnection calcConnection = calculator.openConnection();
             BufferedReader streamReader = new BufferedReader(new InputStreamReader(calcConnection.getInputStream()));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
                    e.printStackTrace();
        }
    }

This work's fine, but sometimes when url is unreachable or return's some error code this method seem's to crash full app.

What i am curious about is, Why this method fails to catch Exception ?

Also, If i add this :

            catch(Throwable th){

            }

Will this method catch every possible error/exception related to what operation's i perform inside it ?

XTop
  • 255
  • 2
  • 9

2 Answers2

4

I'm not a Java expert, but you should consider that there is a difference between exception and error. Did you have a look here?

May you post the stacktrace? Thank you

Community
  • 1
  • 1
gpicchiarelli
  • 454
  • 6
  • 16
0

If you add this : catch(Throwable th){ } every single exception your method throws will be catch. In order to resolve your problem properly, you must pay atention about what type of exception is thrown when the problem you are describing happens!

Jeyvison
  • 190
  • 1
  • 14
  • 1
    As the name suggests, *anything* that can be `throw`n is by definition an instance of `Throwable` (or `null`). Catching `Throwable` will thus catch anything that can be caught at all. – JimmyB Mar 28 '13 at 18:30