2

I wonder how is it possible to catch two exceptions which I have already caught separately?

    private static boolean checkParameters(Scanner scnr) {
        boolean result = true;
        return result;
    }

private static MnthInYear createMonthInYear() throws IllegalArgumentException { 
        String a = JOptionPane.showInputDialog(null, "Enter month and year");
        Scanner sc = new Scanner(a);
        MnthInYear obj = null;
        if (checkParameters(sc)) {
            try {
                obj = new MnthInYear(sc.next(), sc.nextInt());
            } catch (IllegalArgumentException exc) {
                JOptionPane.showMessageDialog(null,"Wrong month!");
            } catch (InputMismatchException exc2) {
                JOptionPane.showMessageDialog(null,"Wrong year!");
            } catch (NoSuchElementException exc3) {
                JOptionPane.showMessageDialog(null,"No data!");
            }
            sc.close();
        }
        return obj; 
    }

And I need to make something like this:

            } catch (IllegalArgumentException AND InputMismatchException) {
                JOptionPane.showMessageDialog(null,"Wrong month and year!");
              }

How can I get this?

petey
  • 16,914
  • 6
  • 65
  • 97
jas97
  • 29
  • 7
  • 3
    use `llegalArgumentException | InputMismatchException` – garnulf Aug 04 '16 at 19:15
  • 3
    Possible duplicate of [Can I catch multiple Java exceptions in the same catch clause?](http://stackoverflow.com/questions/3495926/can-i-catch-multiple-java-exceptions-in-the-same-catch-clause) – Vince Aug 04 '16 at 19:17
  • 2
    Your error message, "wrong month AND year," seems to suggest that `new MnthInYear(...)` will throw both exceptions if both problems exist. But that's not possible. No single method activation can ever throw more than one Exception object. – Solomon Slow Aug 04 '16 at 19:21
  • You will only throw one exception to catch. If you want an exception for both month and year, you will need to roll a custom exception. – Compass Aug 04 '16 at 19:22
  • "it is already handled by the catch block for exception" – jas97 Aug 04 '16 at 19:25

3 Answers3

1

Use this example

For this:

catch (IOException ex) {
   logger.log(ex);
   throw ex;
catch (SQLException ex) {
   logger.log(ex);
   throw ex;
}

Use this:

catch (IOException|SQLException ex) {
   logger.log(ex);
   throw ex;
}

Visit http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html

Miguel Sánchez
  • 115
  • 1
  • 10
  • Not what the original question was asking. He's asking if it's possible for a block of code to throw two exceptions concurrently and to catch them both simultaneously (as opposed to catching one or the other). – nasukkin Aug 04 '16 at 19:25
  • Upvoting cause I think the downvote is unfair, question is a bit obscure. Yes question seems to want to catch two exceptions at the same time, but it's not obvious (and not supported or really coherent), so upvote. – Taylor Aug 04 '16 at 19:26
1

Technically, no, you can't do that. Once one exception is thrown, control moves to the relevant catch block and the next one won't be thrown. What you probably want instead is to implement a validation pattern where you validate the input, aggregate any errors with the input, and then summarize it at the end of validation with a single message.

nasukkin
  • 2,460
  • 1
  • 12
  • 19
  • Have a look at this article, it's nicely explained there: http://martinfowler.com/articles/replaceThrowWithNotification.html – jhyot Aug 06 '16 at 09:15
0

I finally found a solution, if anyone is interested in.

private static boolean checkParameters(Scanner scnr) {
        boolean result = true;
        String msgError = "Invalid";
        
        try {
            month = scnr.next();
            mnth = MnthInYear.Month.valueOf(month);//class MnthInYear defines enum Month
        } catch (IllegalArgumentException exc) {
            msgError += "month!";
            result = false;
        }
        try {
            year = scnr.nextInt();
            if (year < 0) {
                msgError += "year!";
                result = false;
            }
        } catch (InputMismatchException exc) {
            msgError += "month and year!";
            result = false;
        }
        if (msgError != "") {
            JOptionPane.showMessageDialog(null,msgError);
        }
        return result;
    }

Thanks for your answers!

jas97
  • 29
  • 7