XTop
XTop

Reputation: 255

Catching Exception in java to make application continue its execution

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 ?

Upvotes: 2

Views: 199

Answers (2)

Jeyvison
Jeyvison

Reputation: 200

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!

Upvotes: 0

gpicchiarelli
gpicchiarelli

Reputation: 452

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

Upvotes: 4

Related Questions