This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 270152 - Re-throw checked exception hint
Summary: Re-throw checked exception hint
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: Dev
Hardware: PC Linux
: P2 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-03-21 12:14 UTC by Jaroslav Tulach
Modified: 2017-03-21 12:15 UTC (History)
3 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jaroslav Tulach 2017-03-21 12:14:42 UTC
NetBeans has a great support that detects exceptions that need to be caught:

public Object create() {
  return Object.class.newInstance();
}

will not compile and the IDE gives me following hints:

Add throws ... (twice)
Surround with try/catch

I certainly don't want to declare the exception, so "Add throws..." isn't an option, so let's do "try/catch". The generated code is:

public Object create() {
        try {
            return Object.class.newInstance();
        } catch (InstantiationException ex) {
            Exceptions.printStackTrace(ex); // either
        } catch (IllegalAccessException ex) { // or
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
}


I've seen the code many times, however in 90% of cases I had to re-write it to:

        try {
            return Object.class.newInstance();
        } catch (InstantiationException ex) {
            throw new IllegalStateException(ex);
        } catch (IllegalAccessException ex) {
            throw new IllegalStateException(ex);
        }

Can't there be a hint "Re-throw (as unchecked) exception" available? Many people would find it useful.

Btw. I would also welcome "Re-throw silently" that would generate

public Object create() {
        try {
            return Object.class.newInstance();
        } catch (InstantiationException ex) {
            throw raise(RuntimeException.class, ex);
        } catch (IllegalAccessException ex) {
            throw raise(RuntimeException.class, ex);
        }
}

private static <E extends Throwable> E raise(Class<E> type, Throwable t) throws E {
    throw (E)t;
}


which I would consider extremely useful, but I can live without it for now.