Reputation: 24630
Ok i have sinned, i wrote too much code like this
try {
// my code
} catch (Exception ex) {
// doesn't matter
}
Now i'm going to cleanup/refactor this.
I'm using NB 6.7 and code completion works fine on first writing, adding all Exception types, etc. Once i have done the above code NB do not give more help.
Do you know a way to say NB look again for all Exception types and make the proposal for handling them and do code completion again ?
Upvotes: 5
Views: 2578
Reputation: 114787
I just can provide the eclipse approach and hope that it's somewhat similiar with netbeans:
You may save your existing exception handling code to paste it in after the refactoring.
Edit
Tom had a very good comment regarding RuntimeException. So the procedure should better look like this:
This will preserve your exception handling of RuntimeExceptions (and subtypes!).
So from
try {
Integer.parseInt("Force a RuntimeException");
myInputStream.close();
} catch (Exception oops) {
// catch both IOException and NumberFormatException
}
you go to
try {
Integer.parseInt("Force a RuntimeException");
myInputStream.close();
} catch (IOException oops) {
// catch IOException
} catch (Exception oops) {
// catch NumberFormatException
}
(although you could manually replace Exception by NumberFormatException in this case, but it's just an example)
Upvotes: 1
Reputation: 24159
When you ask for a proposal on how to handle the exceptions ...
There is no generally accepted way to handle them. Otherwise, you bet the java language would have implicitly that behavior.
Exceptions exist in two forms, by design:
At each level of code (method or block), the code has to choose what to do, in the event of any exception (except unchecked exceptions that can omit the treatment altogether). This is a choice of responsibility that varies, there is no decision valid for all cases :
The java language lets you have specific syntaxes making easier to handle exceptions, like the catch of specific exceptions followed more general ones...
Typically, you consider Exceptions in your architecture, and make some design decisions. Some examples (mixed in unique ways):
Upvotes: 1
Reputation: 597134
PMD identifies all these places where you have empty catch
blocks (PMD does much more actually). It has NetBeans integration, so give it a try.
After identifying all the places with empty catch
blocks you will have to consider each one by itself:
NullPointerException
, add a null
check instead.Upvotes: 4
Reputation: 72514
The problem is, that your catch-all handler "handles" all the exceptions, so there's not need for Netbeans to show any more hints.
If your exception handlers are already empty and you are planning to refactor them anyway, you can just temporarily remove them.
Tip: Auto-format your code, search for try
and use bracket highlighting to find the matching catch
blocks. Then remove all the handling code.
After that Netbeans will again propose various actions to handle the possible exceptions.
PS: Be careful, the default handling of Netbeans (i.e. just logging) is not always the best choice, either.
Upvotes: 3