java - 尽管 try-catch 出现异常错误并抛出异常尝试
问题描述
我很困惑,因为我在下面有这段代码,我相信它应该捕获异常:
@SuppressWarnings("unchecked")
public static String method(String userToken, int nthRecent) throws Exception
{
//my own user Token: 35a5b026870c49f69d3c688779ff26db
String tweet = tweets.get(nthRecent);
try
{
String urlString = "https://api.dandelion.eu/datatxt/sent/v1/?lang=en&text=";
urlString += tweet;
urlString = urlString + "&token="+userToken;
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuffer response = new StringBuffer();
String line;
while ((line = in.readLine()) != null)
{
response.append(line);
}
in.close();
return response.toString();
con.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
但我不断收到此错误消息:
TwitterPart3.java:42:错误:未报告的异常异常;必须被捕获或声明被抛出 consolePrint.println(bigBird.method(userToken, nthRecent));
我是否抛出了错误的异常?我已经尝试过 try-catch 和 throwing,所以我不确定为什么会这样。
更新:以下是我的主要方法:
private static PrintStream consolePrint;
public static void main(String[] args) throws TwitterException, IOException
{
consolePrint = System.out;
SentimentAnalyser bigBird = new SentimentAnalyser(consolePrint);
Scanner scan = new Scanner(System.in);
consolePrint.print("Please enter a Twitter handle, do not include the @symbol --> ");
String twitter_handle = scan.next();
while(!twitter_handle.equals("done"))
{
bigBird.queryHandle(twitter_handle);
consolePrint.print("Enter the \"nth\" most recent tweet you would like analysed from " + twitter_handle + "--> ");
int nthRecent = scan.nextInt();
consolePrint.print("Enter your user Token " + twitter_handle + "--> ");
String userToken = scan.next();
consolePrint.println(bigBird.method(userToken, nthRecent)); // this is the line 42 the error refers to
twitter_handle = scan.next();
}
}
解决方案
推荐阅读
- javascript - 如何获取条件渲染列表的特定实例的大小(高度/宽度)?
- aspnetboilerplate - Mvc.Authorization.AbpAuthorizationFilter - System.ObjectDisposedException:无法访问已处置的对象。对象名称:'UserManagerProxy'
- ios - 创建响铃/静音通知视图 ios13
- node.js - 在 javascript 中将八位字节流数据保存为 PDF 文件
- css - 在 Css svg Animation 中,我无法为 Á 对象设置动画以遵循路径
- java - 如何根据 Apache POI 中的“名称框”值查找列索引号
- r - 删除一行并对R中的数据框进行排序
- objective-c - 使用 UIImagePickerController 在 iOS 13 上使我的应用程序崩溃
- django - 如何从外部应用程序调用外键,其中键是 Django 中的非主键?
- mongodb - GraphQL 和 MongoDB - 从 mongodb 自动插入的数据中获取“createdAt”字段