Java 异常错误,尽管尝试捕获并抛出异常尝试
我非常困惑,因为我有下面的代码,我认为应该捕获异常:Java 异常错误,尽管尝试捕获并抛出异常尝试,java,exception,try-catch,throw,Java,Exception,Try Catch,Throw,我非常困惑,因为我有下面的代码,我认为应该捕获异常: @SuppressWarnings(“未选中”) 公共静态字符串方法(String userToken,int-nthRecent)引发异常 { //我自己的用户令牌:35a5b026870c49f69d3c688779ff26db String tweet=tweets.get(最近); 尝试 { 字符串URL字符串=”https://api.dandelion.eu/datatxt/sent/v1/?lang=en&text="; ur
@SuppressWarnings(“未选中”)
公共静态字符串方法(String userToken,int-nthRecent)引发异常
{
//我自己的用户令牌:35a5b026870c49f69d3c688779ff26db
String tweet=tweets.get(最近);
尝试
{
字符串URL字符串=”https://api.dandelion.eu/datatxt/sent/v1/?lang=en&text=";
urlString+=tweet;
urlString=urlString+“&token=“+userToken;
URL=新URL(URL字符串);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“GET”);
BufferedReader in=新的BufferedReader(新的InputStreamReader(con.getInputStream());
StringBuffer响应=新的StringBuffer();
弦线;
而((line=in.readLine())!=null)
{
响应。追加(行);
}
in.close();
返回response.toString();
con.disconnect();
}
捕获(例外e)
{
e、 printStackTrace();
返回null;
}
}
这是TwitterPart3的第42行?啊,第42行实际上是主方法方法
被声明为抛出异常
,它的调用者必须考虑到这一点。或者,只需删除抛出异常
@singularity:从调用方方法main
捕获它,或者将调用方方法声明为抛出异常。或者从方法()
中删除抛出异常
,因为它可能从未抛出异常。