1

I am dealing with JaudioTagger API to manipulate MP3 files, I have to repeat the following exceptions over and over again... I was thinking of having a generic exception handler where I could forward each exception maybe with a flag number and the generic method would be deal with it by having different switch cases maybe ? Is it possible ? I would really appreciate if someone could give the method signature or a way to call it

} catch (CannotReadException ex) {
                Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ReadOnlyFileException ex) {
                Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvalidAudioFrameException ex) {
                Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TagException ex) {
                Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
1
  • What do you mean with "repeat exceptions"? Commented Jul 8, 2012 at 21:23

2 Answers 2

5

Pre-JDK 7 all you can do is write a utility function and call it from each of the catch blocks:

private void handle(Exception ex) {
    Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}

private void someOtherMethod() {
    try {
        // something that might throw
    } catch (CannotReadException ex) {
        handle(ex);
    } catch (ReadOnlyFileException ex) {
        handle(ex);
    } catch (IOException ex) {
        handle(ex);
    } catch (InvalidAudioFrameException ex) {
        handle(ex);
    } catch (TagException ex) {
        handle(ex);
    }
}

Starting in JDK 7, you can use multi-catch:

private void someOtherMethod() {
    try {
        // something that might throw
    } catch (CannotReadException | ReadOnlyFileException | IOException
             | InvalidAudioFrameException | TagException ex) {
        Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

See "Catching multiple exceptions".

Sign up to request clarification or add additional context in comments.

Comments

0

This answer is obsolete starting with Java 7. Use multi-catch like John Watts shows in his answer.

I suggest using

try {
    /* ... your code here ... */
} catch (Exception ex) {
    handle(ex);
}

And handle it this way: (you have to replace the OtherException that you don't handle or remove the throws)

private static void handle(Exception ex) throws SomeOtherException {
    if (ex instanceof CannotReadException || ex instanceof ReadOnlyFileException) {
        Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
    } else if (ex instanceof SomeOtherException) {
        throw (SomeOtherException) ex;
    } else if (ex instanceof RuntimeException) {
        throw (RuntimeException) ex;
    } else {
        throw new RuntimeException(ex);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.