Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6412514

Message "except.never.thrown.in.try" have to be reported in extra cases

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 7
    • 6
    • specification
    • rc
    • x86
    • windows_xp
    • Verified

      A DESCRIPTION OF THE REQUEST :
      1. Message "except.never.thrown.in.try" have to be reported for "checked" catch clauses if all possible exceptions (checked and unchecked), which are thrown in try clause and which are to be caught in this catch clause, are already caught in previous catch statements.
      For example:

      public static void simple() {
      try {
      throw new java.io.FileNotFoundException();
      }
      catch(java.io.FileNotFoundException exc) {
      }
      catch(java.io.IOException exc) { // warning: already caught
      }
      }

      public static void nothing_runtime_exception() {
      try {
      // nothing
      }
      catch(RuntimeException exc) {
      }
      catch(Exception exc) { // warning: no checked exceptions, runtime exception is already caught
      }
      }


      2. Message "except.never.thrown.in.try" have to be warning, not error.
      It is necessary for two reasons:
      a) Language compatibility: existing programs have to compile even they fall into that extra cases, in which "except.never.thrown.in.try" is going to be reported.
      b) It's possible to throw checked exception in unchecked way. Therefore if catch clause looks unreachable, it may work anyway.

      import java.io.IOException;

      public class Main {
      public static void main(String[] args) {
      try {
      class A {
      A() throws IOException {
      throw new IOException();
      }
      }
      A.class.newInstance();
      }
      catch(IOException exc) { // it looks unreal but it's possible
      System.err.println("catch(IOException exc)");
      exc.printStackTrace();
      }
      catch(Exception exc) {
      System.err.println("catch(Exception exc)");
      exc.printStackTrace();
      }
      }
      }


      JUSTIFICATION :
      already described above


      ---------- BEGIN SOURCE ----------
      public class Main {

      public static void simple() {
      try {
      throw new java.io.FileNotFoundException();
      }
      catch(java.io.FileNotFoundException exc) {
      }
      catch(java.io.IOException exc) { // warning: simple -- already caught
      }
      }

      public static void nothing_exception() {
      try {
      nothing();
      }
      catch(Exception exc) { // no report: nothing_exception
      }
      }

      public static void checked_exception() {
      try {
      checked();
      }
      catch(Exception exc) { // no report: checked_exception
      }
      }

      public static void runtime_exception() {
      try {
      runtime();
      }
      catch(Exception exc) { // no report: runtime_exception
      }
      }

      public static void nothing_throwable() {
      try {
      nothing();
      }
      catch(Throwable exc) { // no report: nothing_throwable
      }
      }

      public static void checked_throwable() {
      try {
      checked();
      }
      catch(Throwable exc) { // no report: checked_throwable
      }
      }

      public static void runtime_throwable() {
      try {
      runtime();
      }
      catch(Throwable exc) { // no report: runtime_throwable
      }
      }

      public static void nothing_error_throwable() {
      try {
      nothing();
      }
      catch(Error exc) {
      }
      catch(Throwable exc) { // no report: nothing_error_throwable
      }
      }

      public static void checked_error_throwable() {
      try {
      checked();
      }
      catch(Error exc) {
      }
      catch(Throwable exc) { // no report: checked_error_throwable
      }
      }

      public static void runtime_error_throwable() {
      try {
      runtime();
      }
      catch(Error exc) {
      }
      catch(Throwable exc) { // no report: runtime_error_throwable
      }
      }

      public static void nothing_runtime_throwable() {
      try {
      nothing();
      }
      catch(RuntimeException exc) {
      }
      catch(Throwable exc) { // no report: nothing_runtime_throwable
      }
      }

      public static void checked_runtime_throwable() {
      try {
      checked();
      }
      catch(RuntimeException exc) {
      }
      catch(Throwable exc) { // no report: checked_runtime_throwable
      }
      }

      public static void runtime_runtime_throwable() {
      try {
      runtime();
      }
      catch(RuntimeException exc) {
      }
      catch(Throwable exc) { // no report: runtime_runtime_throwable
      }
      }

      public static void nothing_runtime_exception() {
      try {
      nothing();
      }
      catch(RuntimeException exc) {
      }
      catch(Exception exc) { // warning: nothing_runtime_exception
      }
      }

      public static void checked_runtime_exception() {
      try {
      checked();
      }
      catch(RuntimeException exc) {
      }
      catch(Exception exc) { // no report: checked_runtime_exception
      }
      }

      public static void checked_runtime_interrupted_exception() {
      try {
      checked();
      }
      catch(RuntimeException exc) {
      }
      catch(InterruptedException exc) {
      }
      catch(Exception exc) { // warning: checked_runtime_interrupted_exception
      }
      }

      public static void runtime_runtime_exception() {
      try {
      runtime();
      }
      catch(RuntimeException exc) {
      }
      catch(Exception exc) { // warning: runtime_runtime_exception
      }
      }

      public static void nothing_runtime_error_throwable() {
      try {
      nothing();
      }
      catch(RuntimeException exc) {
      }
      catch(Error exc) {
      }
      catch(Throwable exc) { // warning: nothing_runtime_error_throwable
      }
      }

      public static void checked_runtime_error_throwable() {
      try {
      checked();
      }
      catch(RuntimeException exc) {
      }
      catch(Error exc) {
      }
      catch(Throwable exc) { // no report: checked_runtime_error_throwable
      }
      }

      public static void checked_runtime_error_interrupted_throwable() {
      try {
      checked();
      }
      catch(RuntimeException exc) {
      }
      catch(Error exc) {
      }
      catch(InterruptedException exc) {
      }
      catch(Throwable exc) { // warning: checked_runtime_error_interrupted_throwable
      }
      }

      public static void runtime_runtime_error_throwable() {
      try {
      runtime();
      }
      catch(RuntimeException exc) {
      }
      catch(Error exc) {
      }
      catch(Throwable exc) { // warning: runtime_runtime_error_throwable
      }
      }

      public static void nothing_interrupted() {
      try {
      nothing();
      }
      catch(InterruptedException exc) { // warning: nothing_interrupted
      }
      }

      public static void checked_interrupted() {
      try {
      checked();
      }
      catch(InterruptedException exc) { // no report: checked_interrupted
      }
      }

      public static void runtime_interrupted() {
      try {
      runtime();
      }
      catch(InterruptedException exc) { // warning: runtime_interrupted
      }
      }

      public static void nothing() {}
      public static void checked() throws InterruptedException {}
      public static void runtime() throws IllegalArgumentException {}
      }

      ---------- END SOURCE ----------

            abuckley Alex Buckley
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: