What Does "Error: Unreported Exception <Xxx>; Must Be Caught or Declared to Be Thrown" Mean and How to Fix It

unreported exception despite of try-catch block

You have declared that the method throws an exception - which it doesn't:

public String fileNotFoundExTest() throws FileNotFoundException {

Just change to:

public String fileNotFoundExTest() {

unreported exception twitter4j.TwitterException; must be caught or declared to be thrown

You need to handle the TwitterException that the code of the initializer block can throw, by wrapping it within a try-catch. Adding throws TwitterException in the main method will not solve the problem, because the exception is not thrown within the main method.

{
try {
ConfigurationBuilder cb = new ConfigurationBuilder();

List <Status> status = twitter.getHomeTimeline();
for(
Status st:status)

{
System.out.println(st.getUser().getName() + "----------" + st.getText());
}
} catch(TwitterException ex) {
... // handle exception
}
}

if this is a console application, you can change the code to use a CommandLineRunner (see https://www.baeldung.com/spring-boot-console-app).



Related Topics



Leave a reply



Submit