TheCrownedPixel
TheCrownedPixel

Reputation: 369

Try - Double Catch Statement Java

I am having an issue not understanding the error message I am getting from my try - catch statement, which has two catches.

I believe everything is correct, however, I am getting an error exception X is never thrown in body of corresponding try statement.

I believe the code is correct, but both of the catch clauses are being rejected!

@PUT
@Path("{id}")
@Consumes("application/json")
public Response updatePerson(@PathParam("id") int id, String is) {
    try {
        Person person = readPerson(is);
        person.setId(id);
        Person locPerson = persons.get(id);
        if(locPerson == null){
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        persons.put(person.getId(), person);

        return Response.status(Response.Status.OK).build();
    } catch (JSONException e) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    } catch (ParseException e) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
}

Upvotes: 0

Views: 3248

Answers (2)

Steve
Steve

Reputation: 11963

it means the exception you are trying to catch is never going to be thrown inside the try block, so its redundant to have it in the catch statement -> warning as error

Upvotes: 0

Eran
Eran

Reputation: 393846

If none of the methods called in your try block may throw JSONException or ParseException, you shouldn't catch those exceptions. That's what exception X is never thrown in body of corresponding try statement means.

Upvotes: 1

Related Questions