public int coderesult (String urlstring) throws Exception {
URL url = new URL(urlstring);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
//System.out.println("Response code is " + httpCon.getResponseCode());
int mycode = httpCon.getResponseCode();
return mycode;
}
public int displaycode () {
int dispcode = 0;
try {
dispcode = coderesult(myurl);
}
catch (MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
}
return dispcode;
}
Android Studio在“dispcode = coderesult(myurl);”中显示错误:“未处理的异常:java.lang.Exception”
答案 0 :(得分:1)
您的codeResult()
方法会抛出Exception
public int coderesult (String urlstring) throws Exception {
因此,您必须专门捕捉catch(Exception ex)
,或声明您从方法中抛出它。
捕获异常时,您要么捕获特定的异常类型,要么捕获更通用的父异常类型。您可以将所有捕获组合成一个catch,因为所有内置的Java异常都从Exception扩展
catch(Exception ex){
ex.printStackTrace();
}
答案 1 :(得分:0)
使用此
public int coderesult (String urlstring) throws IOException
并写
catch(IOException ie) {
ie.printStackTrace();
}