goseta
goseta

Reputation: 790

Android unhandled exception error

I'm try to figure out this for a while, so I have a method that is calling a count() method that is suppose to throw and exception

the count() method

public int count() throws ParseException {
  return something that may throw the ParseException
}

and then calling from here

ParseQuery<ParseObject> query = ParseQuery.getQuery(className);
query.fromLocalDatastore();

int result = 0;

try {
   result = query.count();
} catch (ParseException e) {
   result = 0;
}

return result;

Now I have been trying different scenarios but no matter what the IDE still not compiling and give me the following error

Error:(254, 11) error: exception ParseException is never thrown in body of corresponding try statement

Error:(253, 33) error: unreported exception ParseException; must be caught or declared to be thrown

in the line result = query.count();

I have no idea what I'm doing wrong, thanks for any help

Upvotes: 1

Views: 776

Answers (1)

KDeogharkar
KDeogharkar

Reputation: 10959

You cannot catch exception that will never thrown by your try block as error suggested

try {
   result = query.count(); // this statement not throwing ParseException
} catch (ParseException e) { // you are trying to catch ParseException that never gonna throw. 
   result = 0;
}

It is like

    try {
           .... code // throws ExceptionA
        }
    catch (ExceptionB e) { // and calling ExceptionB

        }

Upvotes: 1

Related Questions