5 Replies Latest reply: Sep 16, 2008 8:39 AM by 807589 RSS

    List of Exceptions as Generics

    807589
      Hi there
      i'm looking for some help with generics and and exception handling.
      Is there a way to do the following:

      public abstract class AnAbstractClass<E1 extends Exception, E2 extends Exception> {
      public abstract void doSomething() throws E1, E2;
      }

      Defining a fix list of exceptions is not really what i want, because it is not known in advance how many excetions are thrown by the implementation (cretain implementations throw more checked Excetions than others). Just throwing java.lang.Exception is not a proper solution either.

      The following does not work either, because E in this case is not throwable, but a List.
      public abstract class AnAbstractClass<E extends List<Exception>> {
      public abstract void doSomething() throws E;
      }
        • 1. Re: List of Exceptions as Generics
          807589
          I don't think generics can help you with this. The standard approach is to define some kind of wrapper exception and wrap any of the various exceptions is one of those.
          • 2. Re: List of Exceptions as Generics
            thomas.behr
            Huh?
            You are designing that method, so it's up to you "how many exceptions" may be thrown by your method.

            My suggestion is to create a custom Exception that can be thrown. If there is an implementation that needs to handle other exceptions, have that implementation catch those, wrap them in your custom exception and throw the custom one.
            abstract class Backup {
              public abstract run() throws BackupException;
            }
            
            class DatabaseBackup extends Backup {
              public run() throws BackupException {
                try {
                  // do some database stuff
                } catch (SQLException e) {
                  throw new BackupException(e);
                }
              } 
            }
            
            class FilesystemBackup extends Backup {
              public run() throws BackupException {
                try {
                  // do some file system stuff
                } catch (IOException e) {
                  throw new BackupException(e);
                }
              } 
            }
            
            class BackupException extends Exception {
              public BackupException(Exception cause) {
                super(cause);
              }
            }
            • 3. Re: List of Exceptions as Generics
              thomas.behr
              malcolmmc wrote:
              I don't think generics can help you with this. The standard approach is to define some kind of wrapper exception and wrap any of the various exceptions is one of those.
              Me too slow. :-(
              • 4. Re: List of Exceptions as Generics
                807589
                Mh this is interessting that generics does not answer to my problem. I don't like the wrapping solution but there does not seem any other solution. thanks all for your answer
                • 5. Re: List of Exceptions as Generics
                  807589
                  Hi Thomas
                  yep i'm designing the method but have no control over the exceptions (allowed excetions on interfaces and the hierarchy of those excetions). In my method i cannot handle these exceptions. Quite silly situation. Seems no other solution around than a wrapper exception.

                  thx.