apache / apache/netbeans

Inform about unnecessary exceptions in the "throws" clause of a method

Open
#4,897 0 comments 0 reactions 0 assignees View on GitHub
Java kind:feature needs:triage
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Description

It would be useful if NetBeans IDE informed about unnecessary checked exceptions in the `throws ` clause of a method. By *unnecessary checked exceptions* I'm referring to checked exceptions that can never be thrown by the body of a method, be it directly (with a `throw` statement) or indirectly (by a call to another method).

### Use case/motivation

Why would this be useful? Because of the following: When we refactor methods, sometimes we leave by mistake exceptions that can't occur anymore. This doesn't cause any error *per se* in the application, but it's confusing when you read the code and also forces the methods up in the call stack to handle or propagate an exception that shouldn't be there.

Someway, the IDE is already aware of which exceptions are thrown in a method's body: if you put the cursor in an exception of the `throws` clause, the IDE highlights which lines may throw it (as @mbien commented on [Java: How to know if a thrown exception is unnecessary?](https://github.com/apache/netbeans/discussions/4086) ).

I'm aware that in the case of a method that overrides an abstract method, it may occur a false positive. Suppose that the abstract method throws an `IOException` but the implementation doesn't throw that exception at all. The IDE might show a warning, even although the abstract method declares such exception (I guess it depends on how much code it analyses). To me, that wouldn't be a big problem: you could just ignore the warning or you could remove the exception from the declaration of the concrete method (the exception would still be there implicitly).

For example, this is what happens when you implement `Appendable.append()`, which declares a `throws IOException` regardless you never throw one.

### Related issues

_No response_

### Are you willing to submit a pull request?

No

### Code of Conduct

Yes

Contributor guide

Open the contributing guide

Research direction

Read the linked discussion and the issue's description of existing exception highlighting, then inspect how NetBeans analyzes Java method bodies and throws clauses. Use the Appendable.append() example to define expected behavior, including methods that override declarations with checked exceptions. Done means the IDE identifies checked exceptions in a method declaration that cannot be thrown by its body without misleading users.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.