public void calculateArea(int length, int width) { if (length <= 0 || width <= 0) { throw new IllegalArgumentException("Length and width must be positive."); } // code to calculate area }
try { // code that may throw IllegalArgumentException } catch (IllegalArgumentException e) { System.out.println(e.getLocalizedMessage()); }In this example, the catch block catches an IllegalArgumentException and displays the localized message using the getLocalizedMessage() method. The IllegalArgumentException class is part of the java.lang package in Java.