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 267523 - Replace with multicatch hint erroneously suggests use of multicatch when generic methods are used in catch blocks
Summary: Replace with multicatch hint erroneously suggests use of multicatch when gene...
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.1
Hardware: PC Linux
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-10 13:31 UTC by josephcz
Modified: 2016-08-10 13:31 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description josephcz 2016-08-10 13:31:02 UTC
The "can be replaced with multicatch" hint shows up in situations where two (seemingly-)identical catch blocks involve a call to a generic method that produces (returns or throws) an Throwable that is typed based on the input type.

The code example below shows a two scenarios of this sort. In this case if you accept either "replace with multicatch" hint it produces code that (due to mixing a RuntimeException with a non-RuntimeException) throws Exception rather than the more specific InvocationTargetException (or IllegalStateException) causing the method which is declared as throwing InvocationTargetException to fail compilation.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

import java.lang.reflect.InvocationTargetException;

public abstract class MyClass {
  public abstract void doSomething () throws InvocationTargetException;

  // SCENARIO 1
  public void myMethod1 () throws InvocationTargetException {
    try {
      doSomething();
    }
    catch (InvocationTargetException e) {  // <-- Shows erroneous "can be replaced with multicatch" hint
      throw logException1(e);
    }
    catch (IllegalStateException e) {
      throw logException1(e);
    }
  }

  public static <T extends Throwable> T logException1 (T e) {
    e.printStackTrace();
    return e;
  }


  // SCENARIO 2
  public void myMethod2 () throws InvocationTargetException {
    try {
      doSomething();
    }
    catch (InvocationTargetException e) {  // <-- Shows erroneous "can be replaced with multicatch" hint
      throw logException2(e);
    }
    catch (IllegalStateException e) {
      throw logException2(e);
    }
  }

  public static <T extends Throwable> RuntimeException logException2 (T e) throws T {
    e.printStackTrace();
    if (e instanceof RuntimeException) return (RuntimeException)e;
    throw e;
  }
}