简体   繁体   English

在Java中捕获异常以使应用程序继续执行

[英]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, 这就是我现在所拥有的,此方法打开的是与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. 这项工作很好,但是有时当url无法访问或返回某些错误代码时,此method似乎会使整个应用程序崩溃。

What i am curious about is, Why this method fails to catch Exception ? 我很好奇的是,为什么此方法无法捕获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 ? method catch与我在其中执行的操作有关的所有可能的error/exception吗?

I'm not a Java expert, but you should consider that there is a difference between exception and error. 我不是Java专家,但是您应该考虑异常和错误之间的区别。 Did you have a look here ? 在这里看看吗?

May you post the stacktrace? 您可以发布stacktrace吗? Thank you 谢谢

If you add this : catch(Throwable th){ } every single exception your method throws will be catch. 如果您添加以下内容: catch(Throwable th){ }您的方法抛出的每个异常都将被捕获。 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! 为了正确解决您的问题,您必须注意当您描述的问题发生时引发的异常类型!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM