-2
public class CatchingExceptions {
    private int erroneousMethod(int p) {
        if (p == 0) {
            throw new IllegalArgumentException();
        }
        int x = 0x01;
        return p / (x >> Math.abs(p)); // this line will throw!
    }

The task is to implement the following method to catch and print the two exceptions.

public void catchExceptions(int passthrough) {

        erroneousMethod(passthrough); // will throw!
        try{
          ????
        } catch (IllegalArgumentException e){
            System.out.println("???? ");
        }
}
Ting Du
  • 1
  • 1
  • Call the method inside the `try` block instead of before it. – Code-Apprentice Apr 05 '21 at 22:19
  • 1
    Does this answer your question? [Can I catch multiple Java exceptions in the same catch clause?](https://stackoverflow.com/questions/3495926/can-i-catch-multiple-java-exceptions-in-the-same-catch-clause) – geocodezip Apr 05 '21 at 23:12

1 Answers1

0

Call the method inside the try block:

public void catchExceptions(int passthrough) {
    try{
        erroneousMethod(passthrough);
    } catch (RuntimeException e) { // catches all unchecked exceptions
        String message = e.getMessage() == null ? "" : (": " + e.getMessage());
        System.out.println(e.getClass().getSimpleName() + ": " + message);
    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722